2

Mktime and other functions give wrong answer for such a date like 2011-02-27 02:04:46;

3

3 Answers 3

10

Just use the strtotime() function, or the DateTime class.


Both the two following portions of code :
echo strtotime('2011-02-27 02:04:46');

$dt = new DateTime('2011-02-27 02:04:46');
echo $dt->format('U');

Will give you the same output :

1298768686
0
2

Use strtotime

$time = '2011-02-27 02:04:46';
strtotime($time);
2
  • 2
    Why the date() call? strtotime already returns a PHP time value, which is a unix timestamp.
    – Marc B
    Commented Apr 21, 2011 at 20:18
  • 1
    my bad, just copied and pasted from a script where I'd used strtotime to convert from one time format to another (not UNIX)
    – bland_dan
    Commented Apr 21, 2011 at 20:21
1

This will also work (if you need this to run from the unix shell):

date +%T

This will show the time like:

14:20:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.