// Simply:$date = date('Y-m-d H:i:s');
// Or:$date = date('Y/m/d H:i:s');
// This would return the date in the following formats respectively:$date = '2012-03-06 17:33:07';// Or$date = '2012/03/06 17:33:07';
/*** This time is based on the default server time zone.* If you want the date in a different time zone,* say if you come from Nairobi, Kenya like I do, you can set* the time zone to Nairobi as shown below.*/
date_default_timezone_set('Africa/Nairobi');
// Then call the date functions$date = date('Y-m-d H:i:s');// Or$date = date('Y/m/d H:i:s');
// date_default_timezone_set() function is however// supported by PHP version 5.1.0 or above.
use \Datetime;
$now = new DateTime();echo $now->format('Y-m-d H:i:s'); // MySQL datetime formatecho $now->getTimestamp(); // Unix Timestamp -- Since PHP 5.3
并指定timezone:
$now = new DateTime(null, new DateTimeZone('America/New_York'));$now->setTimezone(new DateTimeZone('Europe/London')); // Another wayecho $now->getTimezone();
// Assuming today is March 10th, 2001, 5:16:18 pm, and that we are in the// Mountain Standard Time (MST) Time Zone
$today = date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm$today = date("m.d.y"); // 03.10.01$today = date("j, n, Y"); // 10, 3, 2001$today = date("Ymd"); // 20010310$today = date('h-i-s, j-m-y, it is w Day'); // 05-16-18, 10-03-01, 1631 1618 6 Satpm01$today = date('\i\t \i\s \t\h\e jS \d\a\y.'); // it is the 10th day.$today = date("D M j G:i:s T Y"); // Sat Mar 10 17:16:18 MST 2001$today = date('H:m:s \m \i\s\ \m\o\n\t\h'); // 17:03:18 m is month$today = date("H:i:s"); // 17:16:18$today = date("Y-m-d H:i:s"); // 2001-03-10 17:16:18 (the MySQL DATETIME format)
// Set the default timezone to use. Available since PHP 5.1date_default_timezone_set('UTC');
// Prints something like: Mondayecho date("l");
// Prints something like: Monday 8th of August 2016 03:12:46 PMecho date('l jS \of F Y h:i:s A');
// Prints: July 1, 2016 is on a Saturdayecho "July 1, 2016 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2016));
/* Use the constants in the format parameter */// Prints something like: Wed, 25 Sep 2013 15:28:57 -0700echo date(DATE_RFC2822);
// Prints something like: 2016-07-01T00:00:00+00:00echo date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000));
<?php// time() returns current time in seconds$in_seconds = time();
// strftime - Format a local time/date according to locale settingsecho strftime("%m/%d/%y", $in_seconds);?>
<?php// Assuming today is March 10th, 2001, 5:16:18 pm, and that we are in the// Mountain Standard Time (MST) Time Zone
$today = date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm$today = date("m.d.y"); // 03.10.01$today = date("j, n, Y"); // 10, 3, 2001$today = date("Ymd"); // 20010310$today = date('h-i-s, j-m-y, it is w Day'); // 05-16-18, 10-03-01, 1631 1618 6 Satpm01$today = date('\i\t \i\s \t\h\e jS \d\a\y.'); // it is the 10th day.$today = date("D M j G:i:s T Y"); // Sat Mar 10 17:16:18 MST 2001$today = date('H:m:s \m \i\s\ \m\o\n\t\h'); // 17:03:18 m is month$today = date("H:i:s"); // 17:16:18$today = date("Y-m-d H:i:s"); // 2001-03-10 17:16:18 (the MySQL DATETIME format)?>
<?phpdate_default_timezone_set("Asia/Kolkata");echo "Today is " . date("Y/m/d") . "<br>";echo "Today is " . date("Y.m.d") . "<br>";echo "Today is " . date("Y-m-d") . "<br>";echo "Today is " . date("l");echo "The time is " . date("h:i:sa");?>
// set the default timezone to use. Available since PHP 5.1date_default_timezone_set('UTC');
// Prints something like: Mondayecho date("l");
// Prints something like: Monday 8th of August 2005 03:12:46 PMecho date('l jS \of F Y h:i:s A');
// Prints: July 1, 2000 is on a Saturdayecho "July 1, 2000 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2000));
/* use the constants in the format parameter */// prints something like: Wed, 25 Sep 2013 15:28:57 -0700echo date(DATE_RFC2822);
// prints something like: 2000-07-01T00:00:00+00:00echo date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000));
<?php// Specified date/time in your computer's time zone.$date = new DateTime('2000-01-01');echo $date->format('Y-m-d H:i:sP') . "\n";
// Specified date/time in the specified time zone.$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));echo $date->format('Y-m-d H:i:sP') . "\n";
// Current date/time in your computer's time zone.$date = new DateTime();echo $date->format('Y-m-d H:i:sP') . "\n";
// Current date/time in the specified time zone.$date = new DateTime(null, new DateTimeZone('Pacific/Nauru'));echo $date->format('Y-m-d H:i:sP') . "\n";
// Using a UNIX timestamp. Notice the result is in the UTC time zone.$date = new DateTime('@946684800');echo $date->format('Y-m-d H:i:sP') . "\n";
// Non-existent values roll over.$date = new DateTime('2000-02-30');echo $date->format('Y-m-d H:i:sP') . "\n";?>