-1

I have dates in the format Y-M-D hour-minute-second 2013-09-03 12:10:28, but I need the unixtimestamp.

PHP has a slough of functions for transforming dates and times, but apprently none that can accept a date and its format and convert it into unixtime. The closest function I found was mktime(), but I would need to parse my current date into each of its components. Is there an easier way?

Don P
  • 60,113
  • 114
  • 300
  • 432

5 Answers5

3

strtotime('2013-09-03 12:10:28');

user1759572
  • 683
  • 4
  • 11
2
<?php
$date = DateTime::createFromFormat('Y-m-d H:i:s', '2013-09-03 12:10:28');
echo $date->getTimestamp();

IMHO it is better not to use strtotime() because it tries to guess a time format. I do not like any "magic" in programming.

Alex
  • 1,605
  • 11
  • 14
  • This is the best option as the format is explicit rather than implied. – Sammitch Sep 03 '13 at 20:57
  • Good if your date format is not dynamic one. – user1759572 Sep 03 '13 at 21:06
  • I agree with other types of format, but `yyyy-mm-dd H:i:s` is way too standard for "magic" to happen ;) If datetime is in [supported date and time formats](http://si1.php.net/datetime.formats) then you don't need to worry about "magic" happening. – Glavić Sep 03 '13 at 21:15
0
<?php
$str = '2013-09-03 12:10:28';
echo strtotime($str);

1378228228

racerror
  • 1,599
  • 8
  • 9
0

You may try strtotime

echo strtotime('2013-09-03 12:10:28'); // 1378210228
The Alpha
  • 143,660
  • 29
  • 287
  • 307
0
<?php 
echo strtotime('1/1/2013');
?>

Output: 1356994800 -> Unixtimestamp

Similar Question here

Community
  • 1
  • 1
Moeed Farooqui
  • 3,604
  • 1
  • 18
  • 23