Mktime and other functions give wrong answer for such a date like 2011-02-27 02:04:46;
Asked
Active
Viewed 2,695 times
2
-
2http://php.net/strtotime – Pekka Apr 21 '11 at 20:14
-
1[PHP Reference: Date and time functions](http://php.net/manual/en/ref.datetime.php) – Pekka Apr 21 '11 at 20:15
-
1Please use the search function before asking: http://stackoverflow.com/search?q=date+to+timestamp+php – Gordon Apr 22 '11 at 09:53
3 Answers
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

Pascal MARTIN
- 395,085
- 80
- 655
- 663
2
Use strtotime
$time = '2011-02-27 02:04:46';
strtotime($time);

bland_dan
- 448
- 3
- 8
- 19
-
2Why the date() call? strtotime already returns a PHP time value, which is a unix timestamp. – Marc B Apr 21 '11 at 20:18
-
1my 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 Apr 21 '11 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

dvanaria
- 6,593
- 22
- 62
- 82