NOW() function in PHP
Is there a PHP function that returns the date & time in the same format as the MySQL function NOW()
?
I know how to do it using date()
, but I am asking if there is a function only for this.
For example to return:
2009-12-01 00:00:00
php datetime time timestamp
add a comment |
Is there a PHP function that returns the date & time in the same format as the MySQL function NOW()
?
I know how to do it using date()
, but I am asking if there is a function only for this.
For example to return:
2009-12-01 00:00:00
php datetime time timestamp
2
Good question. Simple but effective at bringing out the below dateTime solutions that every programmer struggles with remembering.
– Sweet Chilly Philly
May 2 '17 at 3:28
add a comment |
Is there a PHP function that returns the date & time in the same format as the MySQL function NOW()
?
I know how to do it using date()
, but I am asking if there is a function only for this.
For example to return:
2009-12-01 00:00:00
php datetime time timestamp
Is there a PHP function that returns the date & time in the same format as the MySQL function NOW()
?
I know how to do it using date()
, but I am asking if there is a function only for this.
For example to return:
2009-12-01 00:00:00
php datetime time timestamp
php datetime time timestamp
edited Apr 5 '15 at 16:20
vaxquis
7,77853958
7,77853958
asked Jan 3 '10 at 17:07
MoeAmineMoeAmine
2,39221317
2,39221317
2
Good question. Simple but effective at bringing out the below dateTime solutions that every programmer struggles with remembering.
– Sweet Chilly Philly
May 2 '17 at 3:28
add a comment |
2
Good question. Simple but effective at bringing out the below dateTime solutions that every programmer struggles with remembering.
– Sweet Chilly Philly
May 2 '17 at 3:28
2
2
Good question. Simple but effective at bringing out the below dateTime solutions that every programmer struggles with remembering.
– Sweet Chilly Philly
May 2 '17 at 3:28
Good question. Simple but effective at bringing out the below dateTime solutions that every programmer struggles with remembering.
– Sweet Chilly Philly
May 2 '17 at 3:28
add a comment |
15 Answers
15
active
oldest
votes
Not besides:
date("Y-m-d H:i:s");
45
Note that you may need to use thedate_default_timezone_set
function
– Michel Ayres
Apr 25 '14 at 13:28
Thanks for this useful tip.
– Sedat Kumcu
Jan 12 '17 at 12:27
is there a function name now() in mysqli ?
– MindRoasterMir
Mar 3 at 13:29
add a comment |
date('Y-m-d H:i:s')
Look here for more details: http://pl.php.net/manual/en/function.date.php
add a comment |
With PHP version >= 5.4 DateTime can do this:-
echo (new DateTime())->format('Y-m-d H:i:s');
See it working.
3
nice idea wrapping the constructor and formatting it right the way.
– acme
Jun 17 '13 at 10:03
at last php started to copy from delphi and c# :)
– Erçin Dedeoğlu
Nov 1 '14 at 10:04
1
this may be nice, but does not answer the question at all, it is not easier nor faster to do it this way
– Asped
Nov 16 '14 at 14:11
6
@Asped In what way does it not answer the question? It is a PHP function that "returns the date & time in the same format as the MySQL function NOW()", which is precisely the question.
– vascowhite
Jan 20 '15 at 10:41
@vascowhite - the question was if there is a specific function for this one purpose. so the answer is NO. all the other possibilities listed here may be working and yours is also nice, but does not help, as the guy asking already knew a way how to do it, but wanted an easier, single-purpose function, which yours is not :)
– Asped
Jan 22 '15 at 11:24
|
show 10 more comments
Use this function:
function getDatetimeNow() {
$tz_object = new DateTimeZone('Brazil/East');
//date_default_timezone_set('Brazil/East');
$datetime = new DateTime();
$datetime->setTimezone($tz_object);
return $datetime->format('Y-m-d h:i:s');
}
add a comment |
Try this:
date("Y-m-d H:i:s");
9
You don't really need the second parameter.... waste of 6 characters IMO.
– Tyler Carter
Jan 3 '10 at 17:13
179
Right, if you waste characters like that, we're going to run out of them.
– Bill Karwin
Jan 3 '10 at 17:28
15
8 characters, actually.
– Henrik Erlandsson
Jun 13 '13 at 9:24
51
considering all the waste around here, I find it pretty shameful he used double quotes
– Augie Gardner
Jul 25 '13 at 16:00
35
Imagine all the characters wasted on commenting on the waste of characters!
– Pwnball
Jul 18 '14 at 11:49
|
show 3 more comments
Short answer
$now = date_create()->format('Y-m-d H:i:s');
Read below for the long answer.
Mimicry in PHP
To mimic the MySQL NOW()
function in PHP you can use date_create()->format('Y-m-d H:i:s')
. This approach allows you to handle time/time-zone manipulations easier then date('Y-m-d H:i:s')
. It is more readable and it works since php 5.2.
$now = date_create('now')->format('Y-m-d H:i:s'); // works in php 5.2 and higher
$now = date_create()->format('Y-m-d H:i:s'); // also works in php 5.2
$now = new DateTime('now')->format('Y-m-d H:i:s'); // syntax error!!!
$now = (new DateTime('now'))->format('Y-m-d H:i:s'); // works in php 5.4 and higher
$now = date('Y-m-d H:i:s'); // works as well, but it's less nice then date_create()
The reason why this works is because the MySQL function NOW()
gives the dateTime value in this format: 'YYYY-MM-DD HH:MM:SS'
. See here: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_now.
An interesting fact is that it's possible to get the datetime format by running this query: SHOW VARIABLES LIKE 'd%e_format'
, the result could be something like this:
Variable_name Value
date_format %Y-%m-%d
datetime_format %Y-%m-%d %H:%i:%
The variables up here are read-only variables. So you can't change it. I guess the MySQL NOW()
function gets it's format from the datetime_format
variable.
date_create()->format() VS date()
The favorable facts of date_create('now')->format('Y-m-d H:i:s')
over date('Y-m-d H:i:s')
are:
- easier to handle time manipulations
- easier to handle timezones
- more o.o.p.
easier to handle time manipulations
date_create()
accepts a relative date/time format (like now
, yesterday
or +1 day
) see this link, example:
$tomorrow = date_create('+1 day')->format('Y-m-d H:i:s');
date()
accepts a relative date/time format as well, like this:
$tomorrow = date('Y-m-d H:i:s', strtotime('+1 day'));
$tomorrow = date('Y-m-d H:i:s', (time() + 86400)); // 86400 seconds = 1 day
easier to handle timezones
When timezones matter then the usage of date_create()->format()
makes a lot more sense then date()
because date()
uses the default time zone which is configured in php.ini
at the date.timezone
directive. (link: http://php.net/manual/en/datetime.configuration.php#ini.date.timezone). It is possible to change the timezone e.g. date_default_timezone_set('Asia/Tokyo');
. But the downside of that is that it will affect all date/time functions. This problem doesn't exists if you are using date_create()->format()
in combination with timezone_open()
.
PHP supports multiple timezones. The funny thing is that it even supports the Arctic circle, and Antarctica. Have you ever heard about Longyearbyen
? If not, then don't worry, I didn't heard about it as well 5 minutes ago. But, the nice thing here is that PHP knows about Arctic/Longyearbyen
.
See a list of all supported timezones:
http://php.net/manual/en/timezones.php.
more o.o.p.
O.O.P. uses state-full Objects. So I prefer to think in this way:
// Create a DateTime Object.
// Use the DateTime that applies for tomorrow.
// Give me the datetime in format 'Y-m-d H:i:s'
$tomorrow = date_create('+1 day')->format('Y-m-d H:i:s');
Then to think in this way:
// Give me a date time string in format 'Y-m-d H:i:s',
// use strtotime() to calculate the Unix timestamp that applies for tomorrow.
$tomorrow = date('Y-m-d H:i:s', strtotime('+1 day'));
Therefore I would say that the date_create()->format()
approach is more readable to me then date()
.
Example of date_create()->format()
I use this approach for my projects if I have to fill an array. Like this:
$array = array(
'name' => 'John',
'date_time' => date_create('now')->format('Y-m-d H:i:s'), // uses the default timezone
'date_time_japan' => date_create('now', timezone_open('Asia/Tokyo'))->format('Y-m-d H:i:s'),
);
add a comment |
I was looking for the same answer, and I have come up with this solution for PHP 5.3 or later:
$dtz = new DateTimeZone("Europe/Madrid"); //Your timezone
$now = new DateTime(date("Y-m-d"), $dtz);
echo $now->format("Y-m-d H:i:s");
add a comment |
MySQL function NOW()
returns the current timestamp. The only way I found for PHP is using the following code.
$curr_timestamp = date('Y-m-d H:i:s');
add a comment |
Use strftime
:
strftime("%F %T");
%F
is the same as%Y-%m-%d
.%T
is the same as%H:%M:%S
.
Here's a demo on ideone.
add a comment |
One more answer I find easy to use:
echo date('c');
// 2015-07-27T00:00:00+02:00
This is ISO 8601 date (added in PHP 5) which MySQL uses
Edit
MySQL 5.7 do not allow timezone in the datetime by default. You can disable the error with SQL_MODE=ALLOW_INVALID_DATES
, se the answer here for more details: https://stackoverflow.com/a/35944059/2103434 But that also means that the timezone will be lost when saving to the database!
By default MySQL uses the system's timezone, and as long as php uses the same timezone you should be okay. In my case CET / UTC+2.
That means that if I insert 2015-07-27T00:00:00+02:00
to the database, only 2015-07-27T00:00:00
will be stored (but that is the correct local time!).
When I load the time back in to php,
$importedDate = new DateTime('2015-07-27T00:00:00')
it will automatically assume it's +02:00
timezone since it's the default. Printing this will be correct again:
echo $importedDate->format('c');
// 2015-07-27T00:00:00+02:00
To be safe, always use UTC on the server, specify it in MySQL and PHP, and then only convert it to your users locale when displaying the date:
date_default_timezone_set('UTC');
$importedDate = new DateTime('2015-07-27T00:00:00+02:00');
echo $importedDate->format('c');
// 2015-07-27T00:00:00+02:00
$importedDate->setTimezone(new DateTimeZone("America/New_York"));
echo $importedDate->format('c');
// 2015-07-26T18:00:00-04:00
add a comment |
Or you can use DateTime
constants:
echo date(DateTime::W3C); // 2005-08-15T15:52:01+00:00
Here's the list of them:
ATOM = "Y-m-dTH:i:sP" ; // -> 2005-08-15T15:52:01+00:00
COOKIE = "l, d-M-Y H:i:s T" ; // -> Monday, 15-Aug-2005 15:52:01 UTC
ISO8601 = "Y-m-dTH:i:sO" ; // -> 2005-08-15T15:52:01+0000
RFC822 = "D, d M y H:i:s O" ; // -> Mon, 15 Aug 05 15:52:01 +0000
RFC850 = "l, d-M-y H:i:s T" ; // -> Monday, 15-Aug-05 15:52:01 UTC
RFC1036 = "D, d M y H:i:s O" ; // -> Mon, 15 Aug 05 15:52:01 +0000
RFC1123 = "D, d M Y H:i:s O" ; // -> Mon, 15 Aug 2005 15:52:01 +0000
RFC2822 = "D, d M Y H:i:s O" ; // -> Mon, 15 Aug 2005 15:52:01 +0000
RFC3339 = "Y-m-dTH:i:sP" ; // -> 2005-08-15T15:52:01+00:00 ( == ATOM)
RFC3339_EXTENDED = "Y-m-dTH:i:s.vP" ; // -> 2005-08-15T15:52:01.000+00:00
RSS = "D, d M Y H:i:s O" ; // -> Mon, 15 Aug 2005 15:52:01 +0000
W3C = "Y-m-dTH:i:sP" ; // -> 2005-08-15T15:52:01+00:00
For debugging I prefer a shorter one though (3v4l.org):
echo date('ymdTHisP'); // 180614T120708+02:00
add a comment |
I like the solution posted by user1786647, I've updated it a little to change the timezone to a function argument and add optional support for passing either a Unix time or datetime string to use for the returned datestamp.
Also includes a fall back for "setTimestamp" for users running version lower than PHP 5.3:
function DateStamp($strDateTime = null, $strTimeZone = "Europe/London") {
$objTimeZone = new DateTimeZone($strTimeZone);
$objDateTime = new DateTime();
$objDateTime->setTimezone($objTimeZone);
if (!empty($strDateTime)) {
$fltUnixTime = (is_string($strDateTime)) ? strtotime($strDateTime) : $strDateTime;
if (method_exists($objDateTime, "setTimestamp")) {
$objDateTime->setTimestamp($fltUnixTime);
}
else {
$arrDate = getdate($fltUnixTime);
$objDateTime->setDate($arrDate['year'], $arrDate['mon'], $arrDate['mday']);
$objDateTime->setTime($arrDate['hours'], $arrDate['minutes'], $arrDate['seconds']);
}
}
return $objDateTime->format("Y-m-d H:i:s");
}
add a comment |
you can use php date function with correct format as parameter,
echo date("Y-m-d H:i:s");
add a comment |
The PHP equivalent is time()
: http://php.net/manual/en/function.time.php
Wrong answer!time() Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
– Matt E.
Mar 17 at 20:58
add a comment |
You can use simplePHP class to do this:
echo $date->now();
This class also provides many useful methods for date addition, subtraction and comparison. You can check the tutorials page for more examples.
add a comment |
protected by Community♦ Dec 20 '15 at 5:57
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
15 Answers
15
active
oldest
votes
15 Answers
15
active
oldest
votes
active
oldest
votes
active
oldest
votes
Not besides:
date("Y-m-d H:i:s");
45
Note that you may need to use thedate_default_timezone_set
function
– Michel Ayres
Apr 25 '14 at 13:28
Thanks for this useful tip.
– Sedat Kumcu
Jan 12 '17 at 12:27
is there a function name now() in mysqli ?
– MindRoasterMir
Mar 3 at 13:29
add a comment |
Not besides:
date("Y-m-d H:i:s");
45
Note that you may need to use thedate_default_timezone_set
function
– Michel Ayres
Apr 25 '14 at 13:28
Thanks for this useful tip.
– Sedat Kumcu
Jan 12 '17 at 12:27
is there a function name now() in mysqli ?
– MindRoasterMir
Mar 3 at 13:29
add a comment |
Not besides:
date("Y-m-d H:i:s");
Not besides:
date("Y-m-d H:i:s");
edited Nov 26 '18 at 15:47
answered Jan 3 '10 at 17:08
troelskntroelskn
91.2k22118139
91.2k22118139
45
Note that you may need to use thedate_default_timezone_set
function
– Michel Ayres
Apr 25 '14 at 13:28
Thanks for this useful tip.
– Sedat Kumcu
Jan 12 '17 at 12:27
is there a function name now() in mysqli ?
– MindRoasterMir
Mar 3 at 13:29
add a comment |
45
Note that you may need to use thedate_default_timezone_set
function
– Michel Ayres
Apr 25 '14 at 13:28
Thanks for this useful tip.
– Sedat Kumcu
Jan 12 '17 at 12:27
is there a function name now() in mysqli ?
– MindRoasterMir
Mar 3 at 13:29
45
45
Note that you may need to use the
date_default_timezone_set
function– Michel Ayres
Apr 25 '14 at 13:28
Note that you may need to use the
date_default_timezone_set
function– Michel Ayres
Apr 25 '14 at 13:28
Thanks for this useful tip.
– Sedat Kumcu
Jan 12 '17 at 12:27
Thanks for this useful tip.
– Sedat Kumcu
Jan 12 '17 at 12:27
is there a function name now() in mysqli ?
– MindRoasterMir
Mar 3 at 13:29
is there a function name now() in mysqli ?
– MindRoasterMir
Mar 3 at 13:29
add a comment |
date('Y-m-d H:i:s')
Look here for more details: http://pl.php.net/manual/en/function.date.php
add a comment |
date('Y-m-d H:i:s')
Look here for more details: http://pl.php.net/manual/en/function.date.php
add a comment |
date('Y-m-d H:i:s')
Look here for more details: http://pl.php.net/manual/en/function.date.php
date('Y-m-d H:i:s')
Look here for more details: http://pl.php.net/manual/en/function.date.php
edited May 10 '18 at 14:23
JakeGould
20.8k85076
20.8k85076
answered Jan 3 '10 at 17:08
hszhsz
107k49206270
107k49206270
add a comment |
add a comment |
With PHP version >= 5.4 DateTime can do this:-
echo (new DateTime())->format('Y-m-d H:i:s');
See it working.
3
nice idea wrapping the constructor and formatting it right the way.
– acme
Jun 17 '13 at 10:03
at last php started to copy from delphi and c# :)
– Erçin Dedeoğlu
Nov 1 '14 at 10:04
1
this may be nice, but does not answer the question at all, it is not easier nor faster to do it this way
– Asped
Nov 16 '14 at 14:11
6
@Asped In what way does it not answer the question? It is a PHP function that "returns the date & time in the same format as the MySQL function NOW()", which is precisely the question.
– vascowhite
Jan 20 '15 at 10:41
@vascowhite - the question was if there is a specific function for this one purpose. so the answer is NO. all the other possibilities listed here may be working and yours is also nice, but does not help, as the guy asking already knew a way how to do it, but wanted an easier, single-purpose function, which yours is not :)
– Asped
Jan 22 '15 at 11:24
|
show 10 more comments
With PHP version >= 5.4 DateTime can do this:-
echo (new DateTime())->format('Y-m-d H:i:s');
See it working.
3
nice idea wrapping the constructor and formatting it right the way.
– acme
Jun 17 '13 at 10:03
at last php started to copy from delphi and c# :)
– Erçin Dedeoğlu
Nov 1 '14 at 10:04
1
this may be nice, but does not answer the question at all, it is not easier nor faster to do it this way
– Asped
Nov 16 '14 at 14:11
6
@Asped In what way does it not answer the question? It is a PHP function that "returns the date & time in the same format as the MySQL function NOW()", which is precisely the question.
– vascowhite
Jan 20 '15 at 10:41
@vascowhite - the question was if there is a specific function for this one purpose. so the answer is NO. all the other possibilities listed here may be working and yours is also nice, but does not help, as the guy asking already knew a way how to do it, but wanted an easier, single-purpose function, which yours is not :)
– Asped
Jan 22 '15 at 11:24
|
show 10 more comments
With PHP version >= 5.4 DateTime can do this:-
echo (new DateTime())->format('Y-m-d H:i:s');
See it working.
With PHP version >= 5.4 DateTime can do this:-
echo (new DateTime())->format('Y-m-d H:i:s');
See it working.
edited Oct 29 '14 at 10:00
answered Jun 13 '13 at 7:38
vascowhitevascowhite
15.9k95070
15.9k95070
3
nice idea wrapping the constructor and formatting it right the way.
– acme
Jun 17 '13 at 10:03
at last php started to copy from delphi and c# :)
– Erçin Dedeoğlu
Nov 1 '14 at 10:04
1
this may be nice, but does not answer the question at all, it is not easier nor faster to do it this way
– Asped
Nov 16 '14 at 14:11
6
@Asped In what way does it not answer the question? It is a PHP function that "returns the date & time in the same format as the MySQL function NOW()", which is precisely the question.
– vascowhite
Jan 20 '15 at 10:41
@vascowhite - the question was if there is a specific function for this one purpose. so the answer is NO. all the other possibilities listed here may be working and yours is also nice, but does not help, as the guy asking already knew a way how to do it, but wanted an easier, single-purpose function, which yours is not :)
– Asped
Jan 22 '15 at 11:24
|
show 10 more comments
3
nice idea wrapping the constructor and formatting it right the way.
– acme
Jun 17 '13 at 10:03
at last php started to copy from delphi and c# :)
– Erçin Dedeoğlu
Nov 1 '14 at 10:04
1
this may be nice, but does not answer the question at all, it is not easier nor faster to do it this way
– Asped
Nov 16 '14 at 14:11
6
@Asped In what way does it not answer the question? It is a PHP function that "returns the date & time in the same format as the MySQL function NOW()", which is precisely the question.
– vascowhite
Jan 20 '15 at 10:41
@vascowhite - the question was if there is a specific function for this one purpose. so the answer is NO. all the other possibilities listed here may be working and yours is also nice, but does not help, as the guy asking already knew a way how to do it, but wanted an easier, single-purpose function, which yours is not :)
– Asped
Jan 22 '15 at 11:24
3
3
nice idea wrapping the constructor and formatting it right the way.
– acme
Jun 17 '13 at 10:03
nice idea wrapping the constructor and formatting it right the way.
– acme
Jun 17 '13 at 10:03
at last php started to copy from delphi and c# :)
– Erçin Dedeoğlu
Nov 1 '14 at 10:04
at last php started to copy from delphi and c# :)
– Erçin Dedeoğlu
Nov 1 '14 at 10:04
1
1
this may be nice, but does not answer the question at all, it is not easier nor faster to do it this way
– Asped
Nov 16 '14 at 14:11
this may be nice, but does not answer the question at all, it is not easier nor faster to do it this way
– Asped
Nov 16 '14 at 14:11
6
6
@Asped In what way does it not answer the question? It is a PHP function that "returns the date & time in the same format as the MySQL function NOW()", which is precisely the question.
– vascowhite
Jan 20 '15 at 10:41
@Asped In what way does it not answer the question? It is a PHP function that "returns the date & time in the same format as the MySQL function NOW()", which is precisely the question.
– vascowhite
Jan 20 '15 at 10:41
@vascowhite - the question was if there is a specific function for this one purpose. so the answer is NO. all the other possibilities listed here may be working and yours is also nice, but does not help, as the guy asking already knew a way how to do it, but wanted an easier, single-purpose function, which yours is not :)
– Asped
Jan 22 '15 at 11:24
@vascowhite - the question was if there is a specific function for this one purpose. so the answer is NO. all the other possibilities listed here may be working and yours is also nice, but does not help, as the guy asking already knew a way how to do it, but wanted an easier, single-purpose function, which yours is not :)
– Asped
Jan 22 '15 at 11:24
|
show 10 more comments
Use this function:
function getDatetimeNow() {
$tz_object = new DateTimeZone('Brazil/East');
//date_default_timezone_set('Brazil/East');
$datetime = new DateTime();
$datetime->setTimezone($tz_object);
return $datetime->format('Y-m-d h:i:s');
}
add a comment |
Use this function:
function getDatetimeNow() {
$tz_object = new DateTimeZone('Brazil/East');
//date_default_timezone_set('Brazil/East');
$datetime = new DateTime();
$datetime->setTimezone($tz_object);
return $datetime->format('Y-m-d h:i:s');
}
add a comment |
Use this function:
function getDatetimeNow() {
$tz_object = new DateTimeZone('Brazil/East');
//date_default_timezone_set('Brazil/East');
$datetime = new DateTime();
$datetime->setTimezone($tz_object);
return $datetime->format('Y-m-d h:i:s');
}
Use this function:
function getDatetimeNow() {
$tz_object = new DateTimeZone('Brazil/East');
//date_default_timezone_set('Brazil/East');
$datetime = new DateTime();
$datetime->setTimezone($tz_object);
return $datetime->format('Y-m-d h:i:s');
}
edited Oct 20 '15 at 18:15
answered Feb 1 '13 at 19:26
user1786647user1786647
37135
37135
add a comment |
add a comment |
Try this:
date("Y-m-d H:i:s");
9
You don't really need the second parameter.... waste of 6 characters IMO.
– Tyler Carter
Jan 3 '10 at 17:13
179
Right, if you waste characters like that, we're going to run out of them.
– Bill Karwin
Jan 3 '10 at 17:28
15
8 characters, actually.
– Henrik Erlandsson
Jun 13 '13 at 9:24
51
considering all the waste around here, I find it pretty shameful he used double quotes
– Augie Gardner
Jul 25 '13 at 16:00
35
Imagine all the characters wasted on commenting on the waste of characters!
– Pwnball
Jul 18 '14 at 11:49
|
show 3 more comments
Try this:
date("Y-m-d H:i:s");
9
You don't really need the second parameter.... waste of 6 characters IMO.
– Tyler Carter
Jan 3 '10 at 17:13
179
Right, if you waste characters like that, we're going to run out of them.
– Bill Karwin
Jan 3 '10 at 17:28
15
8 characters, actually.
– Henrik Erlandsson
Jun 13 '13 at 9:24
51
considering all the waste around here, I find it pretty shameful he used double quotes
– Augie Gardner
Jul 25 '13 at 16:00
35
Imagine all the characters wasted on commenting on the waste of characters!
– Pwnball
Jul 18 '14 at 11:49
|
show 3 more comments
Try this:
date("Y-m-d H:i:s");
Try this:
date("Y-m-d H:i:s");
edited May 10 '18 at 14:23
JakeGould
20.8k85076
20.8k85076
answered Jan 3 '10 at 17:10
streetparadestreetparade
13.9k3291119
13.9k3291119
9
You don't really need the second parameter.... waste of 6 characters IMO.
– Tyler Carter
Jan 3 '10 at 17:13
179
Right, if you waste characters like that, we're going to run out of them.
– Bill Karwin
Jan 3 '10 at 17:28
15
8 characters, actually.
– Henrik Erlandsson
Jun 13 '13 at 9:24
51
considering all the waste around here, I find it pretty shameful he used double quotes
– Augie Gardner
Jul 25 '13 at 16:00
35
Imagine all the characters wasted on commenting on the waste of characters!
– Pwnball
Jul 18 '14 at 11:49
|
show 3 more comments
9
You don't really need the second parameter.... waste of 6 characters IMO.
– Tyler Carter
Jan 3 '10 at 17:13
179
Right, if you waste characters like that, we're going to run out of them.
– Bill Karwin
Jan 3 '10 at 17:28
15
8 characters, actually.
– Henrik Erlandsson
Jun 13 '13 at 9:24
51
considering all the waste around here, I find it pretty shameful he used double quotes
– Augie Gardner
Jul 25 '13 at 16:00
35
Imagine all the characters wasted on commenting on the waste of characters!
– Pwnball
Jul 18 '14 at 11:49
9
9
You don't really need the second parameter.... waste of 6 characters IMO.
– Tyler Carter
Jan 3 '10 at 17:13
You don't really need the second parameter.... waste of 6 characters IMO.
– Tyler Carter
Jan 3 '10 at 17:13
179
179
Right, if you waste characters like that, we're going to run out of them.
– Bill Karwin
Jan 3 '10 at 17:28
Right, if you waste characters like that, we're going to run out of them.
– Bill Karwin
Jan 3 '10 at 17:28
15
15
8 characters, actually.
– Henrik Erlandsson
Jun 13 '13 at 9:24
8 characters, actually.
– Henrik Erlandsson
Jun 13 '13 at 9:24
51
51
considering all the waste around here, I find it pretty shameful he used double quotes
– Augie Gardner
Jul 25 '13 at 16:00
considering all the waste around here, I find it pretty shameful he used double quotes
– Augie Gardner
Jul 25 '13 at 16:00
35
35
Imagine all the characters wasted on commenting on the waste of characters!
– Pwnball
Jul 18 '14 at 11:49
Imagine all the characters wasted on commenting on the waste of characters!
– Pwnball
Jul 18 '14 at 11:49
|
show 3 more comments
Short answer
$now = date_create()->format('Y-m-d H:i:s');
Read below for the long answer.
Mimicry in PHP
To mimic the MySQL NOW()
function in PHP you can use date_create()->format('Y-m-d H:i:s')
. This approach allows you to handle time/time-zone manipulations easier then date('Y-m-d H:i:s')
. It is more readable and it works since php 5.2.
$now = date_create('now')->format('Y-m-d H:i:s'); // works in php 5.2 and higher
$now = date_create()->format('Y-m-d H:i:s'); // also works in php 5.2
$now = new DateTime('now')->format('Y-m-d H:i:s'); // syntax error!!!
$now = (new DateTime('now'))->format('Y-m-d H:i:s'); // works in php 5.4 and higher
$now = date('Y-m-d H:i:s'); // works as well, but it's less nice then date_create()
The reason why this works is because the MySQL function NOW()
gives the dateTime value in this format: 'YYYY-MM-DD HH:MM:SS'
. See here: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_now.
An interesting fact is that it's possible to get the datetime format by running this query: SHOW VARIABLES LIKE 'd%e_format'
, the result could be something like this:
Variable_name Value
date_format %Y-%m-%d
datetime_format %Y-%m-%d %H:%i:%
The variables up here are read-only variables. So you can't change it. I guess the MySQL NOW()
function gets it's format from the datetime_format
variable.
date_create()->format() VS date()
The favorable facts of date_create('now')->format('Y-m-d H:i:s')
over date('Y-m-d H:i:s')
are:
- easier to handle time manipulations
- easier to handle timezones
- more o.o.p.
easier to handle time manipulations
date_create()
accepts a relative date/time format (like now
, yesterday
or +1 day
) see this link, example:
$tomorrow = date_create('+1 day')->format('Y-m-d H:i:s');
date()
accepts a relative date/time format as well, like this:
$tomorrow = date('Y-m-d H:i:s', strtotime('+1 day'));
$tomorrow = date('Y-m-d H:i:s', (time() + 86400)); // 86400 seconds = 1 day
easier to handle timezones
When timezones matter then the usage of date_create()->format()
makes a lot more sense then date()
because date()
uses the default time zone which is configured in php.ini
at the date.timezone
directive. (link: http://php.net/manual/en/datetime.configuration.php#ini.date.timezone). It is possible to change the timezone e.g. date_default_timezone_set('Asia/Tokyo');
. But the downside of that is that it will affect all date/time functions. This problem doesn't exists if you are using date_create()->format()
in combination with timezone_open()
.
PHP supports multiple timezones. The funny thing is that it even supports the Arctic circle, and Antarctica. Have you ever heard about Longyearbyen
? If not, then don't worry, I didn't heard about it as well 5 minutes ago. But, the nice thing here is that PHP knows about Arctic/Longyearbyen
.
See a list of all supported timezones:
http://php.net/manual/en/timezones.php.
more o.o.p.
O.O.P. uses state-full Objects. So I prefer to think in this way:
// Create a DateTime Object.
// Use the DateTime that applies for tomorrow.
// Give me the datetime in format 'Y-m-d H:i:s'
$tomorrow = date_create('+1 day')->format('Y-m-d H:i:s');
Then to think in this way:
// Give me a date time string in format 'Y-m-d H:i:s',
// use strtotime() to calculate the Unix timestamp that applies for tomorrow.
$tomorrow = date('Y-m-d H:i:s', strtotime('+1 day'));
Therefore I would say that the date_create()->format()
approach is more readable to me then date()
.
Example of date_create()->format()
I use this approach for my projects if I have to fill an array. Like this:
$array = array(
'name' => 'John',
'date_time' => date_create('now')->format('Y-m-d H:i:s'), // uses the default timezone
'date_time_japan' => date_create('now', timezone_open('Asia/Tokyo'))->format('Y-m-d H:i:s'),
);
add a comment |
Short answer
$now = date_create()->format('Y-m-d H:i:s');
Read below for the long answer.
Mimicry in PHP
To mimic the MySQL NOW()
function in PHP you can use date_create()->format('Y-m-d H:i:s')
. This approach allows you to handle time/time-zone manipulations easier then date('Y-m-d H:i:s')
. It is more readable and it works since php 5.2.
$now = date_create('now')->format('Y-m-d H:i:s'); // works in php 5.2 and higher
$now = date_create()->format('Y-m-d H:i:s'); // also works in php 5.2
$now = new DateTime('now')->format('Y-m-d H:i:s'); // syntax error!!!
$now = (new DateTime('now'))->format('Y-m-d H:i:s'); // works in php 5.4 and higher
$now = date('Y-m-d H:i:s'); // works as well, but it's less nice then date_create()
The reason why this works is because the MySQL function NOW()
gives the dateTime value in this format: 'YYYY-MM-DD HH:MM:SS'
. See here: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_now.
An interesting fact is that it's possible to get the datetime format by running this query: SHOW VARIABLES LIKE 'd%e_format'
, the result could be something like this:
Variable_name Value
date_format %Y-%m-%d
datetime_format %Y-%m-%d %H:%i:%
The variables up here are read-only variables. So you can't change it. I guess the MySQL NOW()
function gets it's format from the datetime_format
variable.
date_create()->format() VS date()
The favorable facts of date_create('now')->format('Y-m-d H:i:s')
over date('Y-m-d H:i:s')
are:
- easier to handle time manipulations
- easier to handle timezones
- more o.o.p.
easier to handle time manipulations
date_create()
accepts a relative date/time format (like now
, yesterday
or +1 day
) see this link, example:
$tomorrow = date_create('+1 day')->format('Y-m-d H:i:s');
date()
accepts a relative date/time format as well, like this:
$tomorrow = date('Y-m-d H:i:s', strtotime('+1 day'));
$tomorrow = date('Y-m-d H:i:s', (time() + 86400)); // 86400 seconds = 1 day
easier to handle timezones
When timezones matter then the usage of date_create()->format()
makes a lot more sense then date()
because date()
uses the default time zone which is configured in php.ini
at the date.timezone
directive. (link: http://php.net/manual/en/datetime.configuration.php#ini.date.timezone). It is possible to change the timezone e.g. date_default_timezone_set('Asia/Tokyo');
. But the downside of that is that it will affect all date/time functions. This problem doesn't exists if you are using date_create()->format()
in combination with timezone_open()
.
PHP supports multiple timezones. The funny thing is that it even supports the Arctic circle, and Antarctica. Have you ever heard about Longyearbyen
? If not, then don't worry, I didn't heard about it as well 5 minutes ago. But, the nice thing here is that PHP knows about Arctic/Longyearbyen
.
See a list of all supported timezones:
http://php.net/manual/en/timezones.php.
more o.o.p.
O.O.P. uses state-full Objects. So I prefer to think in this way:
// Create a DateTime Object.
// Use the DateTime that applies for tomorrow.
// Give me the datetime in format 'Y-m-d H:i:s'
$tomorrow = date_create('+1 day')->format('Y-m-d H:i:s');
Then to think in this way:
// Give me a date time string in format 'Y-m-d H:i:s',
// use strtotime() to calculate the Unix timestamp that applies for tomorrow.
$tomorrow = date('Y-m-d H:i:s', strtotime('+1 day'));
Therefore I would say that the date_create()->format()
approach is more readable to me then date()
.
Example of date_create()->format()
I use this approach for my projects if I have to fill an array. Like this:
$array = array(
'name' => 'John',
'date_time' => date_create('now')->format('Y-m-d H:i:s'), // uses the default timezone
'date_time_japan' => date_create('now', timezone_open('Asia/Tokyo'))->format('Y-m-d H:i:s'),
);
add a comment |
Short answer
$now = date_create()->format('Y-m-d H:i:s');
Read below for the long answer.
Mimicry in PHP
To mimic the MySQL NOW()
function in PHP you can use date_create()->format('Y-m-d H:i:s')
. This approach allows you to handle time/time-zone manipulations easier then date('Y-m-d H:i:s')
. It is more readable and it works since php 5.2.
$now = date_create('now')->format('Y-m-d H:i:s'); // works in php 5.2 and higher
$now = date_create()->format('Y-m-d H:i:s'); // also works in php 5.2
$now = new DateTime('now')->format('Y-m-d H:i:s'); // syntax error!!!
$now = (new DateTime('now'))->format('Y-m-d H:i:s'); // works in php 5.4 and higher
$now = date('Y-m-d H:i:s'); // works as well, but it's less nice then date_create()
The reason why this works is because the MySQL function NOW()
gives the dateTime value in this format: 'YYYY-MM-DD HH:MM:SS'
. See here: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_now.
An interesting fact is that it's possible to get the datetime format by running this query: SHOW VARIABLES LIKE 'd%e_format'
, the result could be something like this:
Variable_name Value
date_format %Y-%m-%d
datetime_format %Y-%m-%d %H:%i:%
The variables up here are read-only variables. So you can't change it. I guess the MySQL NOW()
function gets it's format from the datetime_format
variable.
date_create()->format() VS date()
The favorable facts of date_create('now')->format('Y-m-d H:i:s')
over date('Y-m-d H:i:s')
are:
- easier to handle time manipulations
- easier to handle timezones
- more o.o.p.
easier to handle time manipulations
date_create()
accepts a relative date/time format (like now
, yesterday
or +1 day
) see this link, example:
$tomorrow = date_create('+1 day')->format('Y-m-d H:i:s');
date()
accepts a relative date/time format as well, like this:
$tomorrow = date('Y-m-d H:i:s', strtotime('+1 day'));
$tomorrow = date('Y-m-d H:i:s', (time() + 86400)); // 86400 seconds = 1 day
easier to handle timezones
When timezones matter then the usage of date_create()->format()
makes a lot more sense then date()
because date()
uses the default time zone which is configured in php.ini
at the date.timezone
directive. (link: http://php.net/manual/en/datetime.configuration.php#ini.date.timezone). It is possible to change the timezone e.g. date_default_timezone_set('Asia/Tokyo');
. But the downside of that is that it will affect all date/time functions. This problem doesn't exists if you are using date_create()->format()
in combination with timezone_open()
.
PHP supports multiple timezones. The funny thing is that it even supports the Arctic circle, and Antarctica. Have you ever heard about Longyearbyen
? If not, then don't worry, I didn't heard about it as well 5 minutes ago. But, the nice thing here is that PHP knows about Arctic/Longyearbyen
.
See a list of all supported timezones:
http://php.net/manual/en/timezones.php.
more o.o.p.
O.O.P. uses state-full Objects. So I prefer to think in this way:
// Create a DateTime Object.
// Use the DateTime that applies for tomorrow.
// Give me the datetime in format 'Y-m-d H:i:s'
$tomorrow = date_create('+1 day')->format('Y-m-d H:i:s');
Then to think in this way:
// Give me a date time string in format 'Y-m-d H:i:s',
// use strtotime() to calculate the Unix timestamp that applies for tomorrow.
$tomorrow = date('Y-m-d H:i:s', strtotime('+1 day'));
Therefore I would say that the date_create()->format()
approach is more readable to me then date()
.
Example of date_create()->format()
I use this approach for my projects if I have to fill an array. Like this:
$array = array(
'name' => 'John',
'date_time' => date_create('now')->format('Y-m-d H:i:s'), // uses the default timezone
'date_time_japan' => date_create('now', timezone_open('Asia/Tokyo'))->format('Y-m-d H:i:s'),
);
Short answer
$now = date_create()->format('Y-m-d H:i:s');
Read below for the long answer.
Mimicry in PHP
To mimic the MySQL NOW()
function in PHP you can use date_create()->format('Y-m-d H:i:s')
. This approach allows you to handle time/time-zone manipulations easier then date('Y-m-d H:i:s')
. It is more readable and it works since php 5.2.
$now = date_create('now')->format('Y-m-d H:i:s'); // works in php 5.2 and higher
$now = date_create()->format('Y-m-d H:i:s'); // also works in php 5.2
$now = new DateTime('now')->format('Y-m-d H:i:s'); // syntax error!!!
$now = (new DateTime('now'))->format('Y-m-d H:i:s'); // works in php 5.4 and higher
$now = date('Y-m-d H:i:s'); // works as well, but it's less nice then date_create()
The reason why this works is because the MySQL function NOW()
gives the dateTime value in this format: 'YYYY-MM-DD HH:MM:SS'
. See here: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_now.
An interesting fact is that it's possible to get the datetime format by running this query: SHOW VARIABLES LIKE 'd%e_format'
, the result could be something like this:
Variable_name Value
date_format %Y-%m-%d
datetime_format %Y-%m-%d %H:%i:%
The variables up here are read-only variables. So you can't change it. I guess the MySQL NOW()
function gets it's format from the datetime_format
variable.
date_create()->format() VS date()
The favorable facts of date_create('now')->format('Y-m-d H:i:s')
over date('Y-m-d H:i:s')
are:
- easier to handle time manipulations
- easier to handle timezones
- more o.o.p.
easier to handle time manipulations
date_create()
accepts a relative date/time format (like now
, yesterday
or +1 day
) see this link, example:
$tomorrow = date_create('+1 day')->format('Y-m-d H:i:s');
date()
accepts a relative date/time format as well, like this:
$tomorrow = date('Y-m-d H:i:s', strtotime('+1 day'));
$tomorrow = date('Y-m-d H:i:s', (time() + 86400)); // 86400 seconds = 1 day
easier to handle timezones
When timezones matter then the usage of date_create()->format()
makes a lot more sense then date()
because date()
uses the default time zone which is configured in php.ini
at the date.timezone
directive. (link: http://php.net/manual/en/datetime.configuration.php#ini.date.timezone). It is possible to change the timezone e.g. date_default_timezone_set('Asia/Tokyo');
. But the downside of that is that it will affect all date/time functions. This problem doesn't exists if you are using date_create()->format()
in combination with timezone_open()
.
PHP supports multiple timezones. The funny thing is that it even supports the Arctic circle, and Antarctica. Have you ever heard about Longyearbyen
? If not, then don't worry, I didn't heard about it as well 5 minutes ago. But, the nice thing here is that PHP knows about Arctic/Longyearbyen
.
See a list of all supported timezones:
http://php.net/manual/en/timezones.php.
more o.o.p.
O.O.P. uses state-full Objects. So I prefer to think in this way:
// Create a DateTime Object.
// Use the DateTime that applies for tomorrow.
// Give me the datetime in format 'Y-m-d H:i:s'
$tomorrow = date_create('+1 day')->format('Y-m-d H:i:s');
Then to think in this way:
// Give me a date time string in format 'Y-m-d H:i:s',
// use strtotime() to calculate the Unix timestamp that applies for tomorrow.
$tomorrow = date('Y-m-d H:i:s', strtotime('+1 day'));
Therefore I would say that the date_create()->format()
approach is more readable to me then date()
.
Example of date_create()->format()
I use this approach for my projects if I have to fill an array. Like this:
$array = array(
'name' => 'John',
'date_time' => date_create('now')->format('Y-m-d H:i:s'), // uses the default timezone
'date_time_japan' => date_create('now', timezone_open('Asia/Tokyo'))->format('Y-m-d H:i:s'),
);
edited Jan 31 at 13:29
answered Nov 8 '16 at 10:30
JulianJulian
1,62922438
1,62922438
add a comment |
add a comment |
I was looking for the same answer, and I have come up with this solution for PHP 5.3 or later:
$dtz = new DateTimeZone("Europe/Madrid"); //Your timezone
$now = new DateTime(date("Y-m-d"), $dtz);
echo $now->format("Y-m-d H:i:s");
add a comment |
I was looking for the same answer, and I have come up with this solution for PHP 5.3 or later:
$dtz = new DateTimeZone("Europe/Madrid"); //Your timezone
$now = new DateTime(date("Y-m-d"), $dtz);
echo $now->format("Y-m-d H:i:s");
add a comment |
I was looking for the same answer, and I have come up with this solution for PHP 5.3 or later:
$dtz = new DateTimeZone("Europe/Madrid"); //Your timezone
$now = new DateTime(date("Y-m-d"), $dtz);
echo $now->format("Y-m-d H:i:s");
I was looking for the same answer, and I have come up with this solution for PHP 5.3 or later:
$dtz = new DateTimeZone("Europe/Madrid"); //Your timezone
$now = new DateTime(date("Y-m-d"), $dtz);
echo $now->format("Y-m-d H:i:s");
edited Apr 9 '14 at 16:41
Peter Mortensen
13.9k1987113
13.9k1987113
answered Jan 22 '14 at 10:42
santisanti
339414
339414
add a comment |
add a comment |
MySQL function NOW()
returns the current timestamp. The only way I found for PHP is using the following code.
$curr_timestamp = date('Y-m-d H:i:s');
add a comment |
MySQL function NOW()
returns the current timestamp. The only way I found for PHP is using the following code.
$curr_timestamp = date('Y-m-d H:i:s');
add a comment |
MySQL function NOW()
returns the current timestamp. The only way I found for PHP is using the following code.
$curr_timestamp = date('Y-m-d H:i:s');
MySQL function NOW()
returns the current timestamp. The only way I found for PHP is using the following code.
$curr_timestamp = date('Y-m-d H:i:s');
edited Sep 8 '15 at 3:57
kiamlaluno
19.6k146481
19.6k146481
answered Apr 24 '15 at 23:08
Shriganesh ShintreShriganesh Shintre
1,43621014
1,43621014
add a comment |
add a comment |
Use strftime
:
strftime("%F %T");
%F
is the same as%Y-%m-%d
.%T
is the same as%H:%M:%S
.
Here's a demo on ideone.
add a comment |
Use strftime
:
strftime("%F %T");
%F
is the same as%Y-%m-%d
.%T
is the same as%H:%M:%S
.
Here's a demo on ideone.
add a comment |
Use strftime
:
strftime("%F %T");
%F
is the same as%Y-%m-%d
.%T
is the same as%H:%M:%S
.
Here's a demo on ideone.
Use strftime
:
strftime("%F %T");
%F
is the same as%Y-%m-%d
.%T
is the same as%H:%M:%S
.
Here's a demo on ideone.
edited Jul 6 '14 at 14:39
Danny Beckett
14.1k1881123
14.1k1881123
answered Nov 12 '13 at 15:43
user669677
add a comment |
add a comment |
One more answer I find easy to use:
echo date('c');
// 2015-07-27T00:00:00+02:00
This is ISO 8601 date (added in PHP 5) which MySQL uses
Edit
MySQL 5.7 do not allow timezone in the datetime by default. You can disable the error with SQL_MODE=ALLOW_INVALID_DATES
, se the answer here for more details: https://stackoverflow.com/a/35944059/2103434 But that also means that the timezone will be lost when saving to the database!
By default MySQL uses the system's timezone, and as long as php uses the same timezone you should be okay. In my case CET / UTC+2.
That means that if I insert 2015-07-27T00:00:00+02:00
to the database, only 2015-07-27T00:00:00
will be stored (but that is the correct local time!).
When I load the time back in to php,
$importedDate = new DateTime('2015-07-27T00:00:00')
it will automatically assume it's +02:00
timezone since it's the default. Printing this will be correct again:
echo $importedDate->format('c');
// 2015-07-27T00:00:00+02:00
To be safe, always use UTC on the server, specify it in MySQL and PHP, and then only convert it to your users locale when displaying the date:
date_default_timezone_set('UTC');
$importedDate = new DateTime('2015-07-27T00:00:00+02:00');
echo $importedDate->format('c');
// 2015-07-27T00:00:00+02:00
$importedDate->setTimezone(new DateTimeZone("America/New_York"));
echo $importedDate->format('c');
// 2015-07-26T18:00:00-04:00
add a comment |
One more answer I find easy to use:
echo date('c');
// 2015-07-27T00:00:00+02:00
This is ISO 8601 date (added in PHP 5) which MySQL uses
Edit
MySQL 5.7 do not allow timezone in the datetime by default. You can disable the error with SQL_MODE=ALLOW_INVALID_DATES
, se the answer here for more details: https://stackoverflow.com/a/35944059/2103434 But that also means that the timezone will be lost when saving to the database!
By default MySQL uses the system's timezone, and as long as php uses the same timezone you should be okay. In my case CET / UTC+2.
That means that if I insert 2015-07-27T00:00:00+02:00
to the database, only 2015-07-27T00:00:00
will be stored (but that is the correct local time!).
When I load the time back in to php,
$importedDate = new DateTime('2015-07-27T00:00:00')
it will automatically assume it's +02:00
timezone since it's the default. Printing this will be correct again:
echo $importedDate->format('c');
// 2015-07-27T00:00:00+02:00
To be safe, always use UTC on the server, specify it in MySQL and PHP, and then only convert it to your users locale when displaying the date:
date_default_timezone_set('UTC');
$importedDate = new DateTime('2015-07-27T00:00:00+02:00');
echo $importedDate->format('c');
// 2015-07-27T00:00:00+02:00
$importedDate->setTimezone(new DateTimeZone("America/New_York"));
echo $importedDate->format('c');
// 2015-07-26T18:00:00-04:00
add a comment |
One more answer I find easy to use:
echo date('c');
// 2015-07-27T00:00:00+02:00
This is ISO 8601 date (added in PHP 5) which MySQL uses
Edit
MySQL 5.7 do not allow timezone in the datetime by default. You can disable the error with SQL_MODE=ALLOW_INVALID_DATES
, se the answer here for more details: https://stackoverflow.com/a/35944059/2103434 But that also means that the timezone will be lost when saving to the database!
By default MySQL uses the system's timezone, and as long as php uses the same timezone you should be okay. In my case CET / UTC+2.
That means that if I insert 2015-07-27T00:00:00+02:00
to the database, only 2015-07-27T00:00:00
will be stored (but that is the correct local time!).
When I load the time back in to php,
$importedDate = new DateTime('2015-07-27T00:00:00')
it will automatically assume it's +02:00
timezone since it's the default. Printing this will be correct again:
echo $importedDate->format('c');
// 2015-07-27T00:00:00+02:00
To be safe, always use UTC on the server, specify it in MySQL and PHP, and then only convert it to your users locale when displaying the date:
date_default_timezone_set('UTC');
$importedDate = new DateTime('2015-07-27T00:00:00+02:00');
echo $importedDate->format('c');
// 2015-07-27T00:00:00+02:00
$importedDate->setTimezone(new DateTimeZone("America/New_York"));
echo $importedDate->format('c');
// 2015-07-26T18:00:00-04:00
One more answer I find easy to use:
echo date('c');
// 2015-07-27T00:00:00+02:00
This is ISO 8601 date (added in PHP 5) which MySQL uses
Edit
MySQL 5.7 do not allow timezone in the datetime by default. You can disable the error with SQL_MODE=ALLOW_INVALID_DATES
, se the answer here for more details: https://stackoverflow.com/a/35944059/2103434 But that also means that the timezone will be lost when saving to the database!
By default MySQL uses the system's timezone, and as long as php uses the same timezone you should be okay. In my case CET / UTC+2.
That means that if I insert 2015-07-27T00:00:00+02:00
to the database, only 2015-07-27T00:00:00
will be stored (but that is the correct local time!).
When I load the time back in to php,
$importedDate = new DateTime('2015-07-27T00:00:00')
it will automatically assume it's +02:00
timezone since it's the default. Printing this will be correct again:
echo $importedDate->format('c');
// 2015-07-27T00:00:00+02:00
To be safe, always use UTC on the server, specify it in MySQL and PHP, and then only convert it to your users locale when displaying the date:
date_default_timezone_set('UTC');
$importedDate = new DateTime('2015-07-27T00:00:00+02:00');
echo $importedDate->format('c');
// 2015-07-27T00:00:00+02:00
$importedDate->setTimezone(new DateTimeZone("America/New_York"));
echo $importedDate->format('c');
// 2015-07-26T18:00:00-04:00
edited Jun 22 '18 at 8:41
answered Jul 20 '15 at 13:07
Richard87Richard87
1,11211225
1,11211225
add a comment |
add a comment |
Or you can use DateTime
constants:
echo date(DateTime::W3C); // 2005-08-15T15:52:01+00:00
Here's the list of them:
ATOM = "Y-m-dTH:i:sP" ; // -> 2005-08-15T15:52:01+00:00
COOKIE = "l, d-M-Y H:i:s T" ; // -> Monday, 15-Aug-2005 15:52:01 UTC
ISO8601 = "Y-m-dTH:i:sO" ; // -> 2005-08-15T15:52:01+0000
RFC822 = "D, d M y H:i:s O" ; // -> Mon, 15 Aug 05 15:52:01 +0000
RFC850 = "l, d-M-y H:i:s T" ; // -> Monday, 15-Aug-05 15:52:01 UTC
RFC1036 = "D, d M y H:i:s O" ; // -> Mon, 15 Aug 05 15:52:01 +0000
RFC1123 = "D, d M Y H:i:s O" ; // -> Mon, 15 Aug 2005 15:52:01 +0000
RFC2822 = "D, d M Y H:i:s O" ; // -> Mon, 15 Aug 2005 15:52:01 +0000
RFC3339 = "Y-m-dTH:i:sP" ; // -> 2005-08-15T15:52:01+00:00 ( == ATOM)
RFC3339_EXTENDED = "Y-m-dTH:i:s.vP" ; // -> 2005-08-15T15:52:01.000+00:00
RSS = "D, d M Y H:i:s O" ; // -> Mon, 15 Aug 2005 15:52:01 +0000
W3C = "Y-m-dTH:i:sP" ; // -> 2005-08-15T15:52:01+00:00
For debugging I prefer a shorter one though (3v4l.org):
echo date('ymdTHisP'); // 180614T120708+02:00
add a comment |
Or you can use DateTime
constants:
echo date(DateTime::W3C); // 2005-08-15T15:52:01+00:00
Here's the list of them:
ATOM = "Y-m-dTH:i:sP" ; // -> 2005-08-15T15:52:01+00:00
COOKIE = "l, d-M-Y H:i:s T" ; // -> Monday, 15-Aug-2005 15:52:01 UTC
ISO8601 = "Y-m-dTH:i:sO" ; // -> 2005-08-15T15:52:01+0000
RFC822 = "D, d M y H:i:s O" ; // -> Mon, 15 Aug 05 15:52:01 +0000
RFC850 = "l, d-M-y H:i:s T" ; // -> Monday, 15-Aug-05 15:52:01 UTC
RFC1036 = "D, d M y H:i:s O" ; // -> Mon, 15 Aug 05 15:52:01 +0000
RFC1123 = "D, d M Y H:i:s O" ; // -> Mon, 15 Aug 2005 15:52:01 +0000
RFC2822 = "D, d M Y H:i:s O" ; // -> Mon, 15 Aug 2005 15:52:01 +0000
RFC3339 = "Y-m-dTH:i:sP" ; // -> 2005-08-15T15:52:01+00:00 ( == ATOM)
RFC3339_EXTENDED = "Y-m-dTH:i:s.vP" ; // -> 2005-08-15T15:52:01.000+00:00
RSS = "D, d M Y H:i:s O" ; // -> Mon, 15 Aug 2005 15:52:01 +0000
W3C = "Y-m-dTH:i:sP" ; // -> 2005-08-15T15:52:01+00:00
For debugging I prefer a shorter one though (3v4l.org):
echo date('ymdTHisP'); // 180614T120708+02:00
add a comment |
Or you can use DateTime
constants:
echo date(DateTime::W3C); // 2005-08-15T15:52:01+00:00
Here's the list of them:
ATOM = "Y-m-dTH:i:sP" ; // -> 2005-08-15T15:52:01+00:00
COOKIE = "l, d-M-Y H:i:s T" ; // -> Monday, 15-Aug-2005 15:52:01 UTC
ISO8601 = "Y-m-dTH:i:sO" ; // -> 2005-08-15T15:52:01+0000
RFC822 = "D, d M y H:i:s O" ; // -> Mon, 15 Aug 05 15:52:01 +0000
RFC850 = "l, d-M-y H:i:s T" ; // -> Monday, 15-Aug-05 15:52:01 UTC
RFC1036 = "D, d M y H:i:s O" ; // -> Mon, 15 Aug 05 15:52:01 +0000
RFC1123 = "D, d M Y H:i:s O" ; // -> Mon, 15 Aug 2005 15:52:01 +0000
RFC2822 = "D, d M Y H:i:s O" ; // -> Mon, 15 Aug 2005 15:52:01 +0000
RFC3339 = "Y-m-dTH:i:sP" ; // -> 2005-08-15T15:52:01+00:00 ( == ATOM)
RFC3339_EXTENDED = "Y-m-dTH:i:s.vP" ; // -> 2005-08-15T15:52:01.000+00:00
RSS = "D, d M Y H:i:s O" ; // -> Mon, 15 Aug 2005 15:52:01 +0000
W3C = "Y-m-dTH:i:sP" ; // -> 2005-08-15T15:52:01+00:00
For debugging I prefer a shorter one though (3v4l.org):
echo date('ymdTHisP'); // 180614T120708+02:00
Or you can use DateTime
constants:
echo date(DateTime::W3C); // 2005-08-15T15:52:01+00:00
Here's the list of them:
ATOM = "Y-m-dTH:i:sP" ; // -> 2005-08-15T15:52:01+00:00
COOKIE = "l, d-M-Y H:i:s T" ; // -> Monday, 15-Aug-2005 15:52:01 UTC
ISO8601 = "Y-m-dTH:i:sO" ; // -> 2005-08-15T15:52:01+0000
RFC822 = "D, d M y H:i:s O" ; // -> Mon, 15 Aug 05 15:52:01 +0000
RFC850 = "l, d-M-y H:i:s T" ; // -> Monday, 15-Aug-05 15:52:01 UTC
RFC1036 = "D, d M y H:i:s O" ; // -> Mon, 15 Aug 05 15:52:01 +0000
RFC1123 = "D, d M Y H:i:s O" ; // -> Mon, 15 Aug 2005 15:52:01 +0000
RFC2822 = "D, d M Y H:i:s O" ; // -> Mon, 15 Aug 2005 15:52:01 +0000
RFC3339 = "Y-m-dTH:i:sP" ; // -> 2005-08-15T15:52:01+00:00 ( == ATOM)
RFC3339_EXTENDED = "Y-m-dTH:i:s.vP" ; // -> 2005-08-15T15:52:01.000+00:00
RSS = "D, d M Y H:i:s O" ; // -> Mon, 15 Aug 2005 15:52:01 +0000
W3C = "Y-m-dTH:i:sP" ; // -> 2005-08-15T15:52:01+00:00
For debugging I prefer a shorter one though (3v4l.org):
echo date('ymdTHisP'); // 180614T120708+02:00
answered Jun 14 '18 at 10:03
CPHPythonCPHPython
2,4221331
2,4221331
add a comment |
add a comment |
I like the solution posted by user1786647, I've updated it a little to change the timezone to a function argument and add optional support for passing either a Unix time or datetime string to use for the returned datestamp.
Also includes a fall back for "setTimestamp" for users running version lower than PHP 5.3:
function DateStamp($strDateTime = null, $strTimeZone = "Europe/London") {
$objTimeZone = new DateTimeZone($strTimeZone);
$objDateTime = new DateTime();
$objDateTime->setTimezone($objTimeZone);
if (!empty($strDateTime)) {
$fltUnixTime = (is_string($strDateTime)) ? strtotime($strDateTime) : $strDateTime;
if (method_exists($objDateTime, "setTimestamp")) {
$objDateTime->setTimestamp($fltUnixTime);
}
else {
$arrDate = getdate($fltUnixTime);
$objDateTime->setDate($arrDate['year'], $arrDate['mon'], $arrDate['mday']);
$objDateTime->setTime($arrDate['hours'], $arrDate['minutes'], $arrDate['seconds']);
}
}
return $objDateTime->format("Y-m-d H:i:s");
}
add a comment |
I like the solution posted by user1786647, I've updated it a little to change the timezone to a function argument and add optional support for passing either a Unix time or datetime string to use for the returned datestamp.
Also includes a fall back for "setTimestamp" for users running version lower than PHP 5.3:
function DateStamp($strDateTime = null, $strTimeZone = "Europe/London") {
$objTimeZone = new DateTimeZone($strTimeZone);
$objDateTime = new DateTime();
$objDateTime->setTimezone($objTimeZone);
if (!empty($strDateTime)) {
$fltUnixTime = (is_string($strDateTime)) ? strtotime($strDateTime) : $strDateTime;
if (method_exists($objDateTime, "setTimestamp")) {
$objDateTime->setTimestamp($fltUnixTime);
}
else {
$arrDate = getdate($fltUnixTime);
$objDateTime->setDate($arrDate['year'], $arrDate['mon'], $arrDate['mday']);
$objDateTime->setTime($arrDate['hours'], $arrDate['minutes'], $arrDate['seconds']);
}
}
return $objDateTime->format("Y-m-d H:i:s");
}
add a comment |
I like the solution posted by user1786647, I've updated it a little to change the timezone to a function argument and add optional support for passing either a Unix time or datetime string to use for the returned datestamp.
Also includes a fall back for "setTimestamp" for users running version lower than PHP 5.3:
function DateStamp($strDateTime = null, $strTimeZone = "Europe/London") {
$objTimeZone = new DateTimeZone($strTimeZone);
$objDateTime = new DateTime();
$objDateTime->setTimezone($objTimeZone);
if (!empty($strDateTime)) {
$fltUnixTime = (is_string($strDateTime)) ? strtotime($strDateTime) : $strDateTime;
if (method_exists($objDateTime, "setTimestamp")) {
$objDateTime->setTimestamp($fltUnixTime);
}
else {
$arrDate = getdate($fltUnixTime);
$objDateTime->setDate($arrDate['year'], $arrDate['mon'], $arrDate['mday']);
$objDateTime->setTime($arrDate['hours'], $arrDate['minutes'], $arrDate['seconds']);
}
}
return $objDateTime->format("Y-m-d H:i:s");
}
I like the solution posted by user1786647, I've updated it a little to change the timezone to a function argument and add optional support for passing either a Unix time or datetime string to use for the returned datestamp.
Also includes a fall back for "setTimestamp" for users running version lower than PHP 5.3:
function DateStamp($strDateTime = null, $strTimeZone = "Europe/London") {
$objTimeZone = new DateTimeZone($strTimeZone);
$objDateTime = new DateTime();
$objDateTime->setTimezone($objTimeZone);
if (!empty($strDateTime)) {
$fltUnixTime = (is_string($strDateTime)) ? strtotime($strDateTime) : $strDateTime;
if (method_exists($objDateTime, "setTimestamp")) {
$objDateTime->setTimestamp($fltUnixTime);
}
else {
$arrDate = getdate($fltUnixTime);
$objDateTime->setDate($arrDate['year'], $arrDate['mon'], $arrDate['mday']);
$objDateTime->setTime($arrDate['hours'], $arrDate['minutes'], $arrDate['seconds']);
}
}
return $objDateTime->format("Y-m-d H:i:s");
}
edited Apr 9 '14 at 16:39
Peter Mortensen
13.9k1987113
13.9k1987113
answered Oct 1 '13 at 18:46
SethSeth
6112
6112
add a comment |
add a comment |
you can use php date function with correct format as parameter,
echo date("Y-m-d H:i:s");
add a comment |
you can use php date function with correct format as parameter,
echo date("Y-m-d H:i:s");
add a comment |
you can use php date function with correct format as parameter,
echo date("Y-m-d H:i:s");
you can use php date function with correct format as parameter,
echo date("Y-m-d H:i:s");
edited Feb 8 '18 at 18:53
answered Feb 8 '18 at 18:47
SomSom
17727
17727
add a comment |
add a comment |
The PHP equivalent is time()
: http://php.net/manual/en/function.time.php
Wrong answer!time() Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
– Matt E.
Mar 17 at 20:58
add a comment |
The PHP equivalent is time()
: http://php.net/manual/en/function.time.php
Wrong answer!time() Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
– Matt E.
Mar 17 at 20:58
add a comment |
The PHP equivalent is time()
: http://php.net/manual/en/function.time.php
The PHP equivalent is time()
: http://php.net/manual/en/function.time.php
answered Mar 5 at 9:19
Marco MarsalaMarco Marsala
91331226
91331226
Wrong answer!time() Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
– Matt E.
Mar 17 at 20:58
add a comment |
Wrong answer!time() Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
– Matt E.
Mar 17 at 20:58
Wrong answer!
time() Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
– Matt E.
Mar 17 at 20:58
Wrong answer!
time() Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
– Matt E.
Mar 17 at 20:58
add a comment |
You can use simplePHP class to do this:
echo $date->now();
This class also provides many useful methods for date addition, subtraction and comparison. You can check the tutorials page for more examples.
add a comment |
You can use simplePHP class to do this:
echo $date->now();
This class also provides many useful methods for date addition, subtraction and comparison. You can check the tutorials page for more examples.
add a comment |
You can use simplePHP class to do this:
echo $date->now();
This class also provides many useful methods for date addition, subtraction and comparison. You can check the tutorials page for more examples.
You can use simplePHP class to do this:
echo $date->now();
This class also provides many useful methods for date addition, subtraction and comparison. You can check the tutorials page for more examples.
edited Apr 11 '14 at 8:42
answered Mar 30 '14 at 10:18
isaisa
5012716
5012716
add a comment |
add a comment |
protected by Community♦ Dec 20 '15 at 5:57
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
2
Good question. Simple but effective at bringing out the below dateTime solutions that every programmer struggles with remembering.
– Sweet Chilly Philly
May 2 '17 at 3:28