icklechurch
10-16-2009, 04:26 PM
Hi,
I know this is probably a stupid question but I can't get my head around the date() and strtotime() functions in PHP.
How can I get the following date format into a Unix timestamp?
I.e. "18/07/2009 11:44:00" into a Unix timestamp? I've tried various combos and just can't get it to work!!
CFMaBiSmAd
10-16-2009, 04:35 PM
Dates using a slash / as the separator must be in the U.S. mm/dd/yyyy format (sorry to all the countries that use dd/mm/yyyy) - http://www.gnu.org/software/tar/manual/html_node/Calendar-date-items.html#SEC116
You either need to use a different separator or break the date into its' separate pieces and put it back together or use the mktime() function.
Is there a good reason why you want to use a Unix Timestamp, given all the problems with using them for most purposes?
abduraooft
10-16-2009, 04:44 PM
echo strtotime(str_replace('/','-','18/07/2009 11:44:00'));
echo '<br/>'. strtotime('2009-07-18 11:44:00'); ?
mlseim
10-16-2009, 04:53 PM
Funny that this topic just came up in another post...
Put this function in your script:
function DateToTimeStamp($date){
// Convert a date in this format: "18/07/2009 11:44:00"
// To a UNIX TimeString (eg. 1255624200)
// Separate the date from the time.
$dt=explode(" ",$date);
// Separate the month, day, year.
$date=explode("/",$dt[0]);
// Separate the Hour from Minute
$time=explode(":",$dt[1]);
// Generate a UNIX TimeStamp
$new_time=@mktime($time[0],$time[1],$time[2],$date[1],$date[0],$date[2]);
return($new_time);
}
And call it like this ...
$textdate = "18/07/2009 11:44:00";
$unixtime = DateToTimeStamp($textdate);
echo $unixtime;