tags:

views:

29

answers:

4

Hi, can anyone advise how i would convert a date like "Tuesday, 22 June, 2010 00:00" to a unix timestamp using strtotime()? I need to also store the hours and minutes and it's not clear if is best done using strtotime. Thanks for any help!

A: 

strtotime() does convert both date and time. It converts a string to a Unix timestamp.

What is the unix time stamp?

The unix time stamp is a way to track time as a running total of seconds. This count starts at the Unix Epoch on January 1st, 1970. Therefore, the unix time stamp is merely the number of seconds between a particular date and the Unix Epoch. This is very useful to computer systems for tracking and sorting dated information in dynamic and distributed applications both online and client side.

http://www.unixtimestamp.com

You can convert a unix timestamp back to a string using date

quantumSoup
A: 

strtotime and date can be used to convert to a UNIX timestamp and extract portions of that timestamp respectively.

The combination should do what you need.

Jason McCreary
+1  A: 

Due to the second ,, strtotime() will currently not understand your date/time format (remove it and it will work properly).

If you have a static format for the date, using strptime() or DateTime::createFromFormat() are more reliable, and will allow other non-datetime strings in the date to be present as long as you've defined them.

echo DateTime::createFromFormat("l, j F, Y H:i","Tuesday, 22 June, 2010 00:00")->format("c");
Wrikken
Thanks Wrikken, I haven't used DateTime::createFromFormat before - very useful.
steve-o
Should be noted that the DateTime methods are only available in PHP 5.3+, however if you take a look at the PHP documentation most methods have "alternatives" in the comments.
evolve
Most `DateTime` methods are available since 5.2, howver, you are correct the `DateTime::format` is available since 5.3, which is now over a year old.
Wrikken
A: 
$string = "Tuesday, 22 June, 2010 00:00";
$dateString = substr($string,0,-5);
$timeArray = explode(':',substr($string,-5));
$timeStamp = strtotime($dateString) + 60*$timeArray[0] + $timeArray[1];
Mchl