Jump to content

Convert to timestamp?


Petsmacker

Recommended Posts

I was hoping to turn this:
[i]Nov 13, 2006, 10:06pm[/i]
into a UNIX timestamp when posted in a form

Bearing in mind its not 24 hours time and the times can not be changed. I'm basically transferring posts from a proboards board to my own site but that means transferring all 19,000 posts so obviously we're looking for ways to speed up the process of the transfer.
Hope you can help.
Link to comment
https://forums.phpfreaks.com/topic/27930-convert-to-timestamp/
Share on other sites

have you tried strtotime?

http://www.php.net/strtotime

if it won't work "out-of-the-box", you may just have to rearrange the parts a little:

[code]function convert($date) {
//format: "Nov 13, 2006, 10:06pm";

//eliminate commas and seperate the parts
$date = explode(" ", str_replace(",", "", $date));

//determine am / pm, remove the text, adjust the hours if necessary
if (substr($date['3'], -2, 2) == "pm") {
list($hour, $minute) = explode(":", rtrim($date[3], 'pm');
$hour += 12;
} else {
list($hour, $minute) = explode(":", rtrim($date[3], 'am');
}

//reconstruct and pass to strtotime
return strtotime($date[2] . "-" . $date[0] . "-" . $date[1] . " " . $hour . ":" . $minute);
}[/code]
Link to comment
https://forums.phpfreaks.com/topic/27930-convert-to-timestamp/#findComment-127741
Share on other sites

I know there's a way of converting a timestamp to time with gmdate:
e.g - $timeworkedout=gmdate("d M Y H:i:s", $time);
No way to convert it back?

Is there a way to have it come out as a variable? I mean I could sort it so that if the 'am' or 'pm' bit is a problem I could have it convert the hour to 24 hour format by having the PHP recognise the letters 'am' or 'pm'.
Link to comment
https://forums.phpfreaks.com/topic/27930-convert-to-timestamp/#findComment-127746
Share on other sites

My code is:

<?
$dates="Jun 2, 2006, 9:13pm";
function convert($date) {
//format: "Nov 13, 2006, 10:06pm";

//eliminate commas and seperate the parts
$date = explode(" ", str_replace(",", "", $dates));

//determine am / pm, remove the text, adjust the hours if necessary
if (substr($date['3'], -2, 2) == "pm") {
list($hour, $minute) = explode(":", rtrim($date[3], 'pm'));
$hour += 12;
} else {
list($hour, $minute) = explode(":", rtrim($date[3], 'am'));
}

//reconstruct and pass to strtotime
return strtotime($date[2] . "-" . $date[0] . "-" . $date[1] . " " . $hour . ":" . $minute);
}
?>

But get no output whatsoever.
Link to comment
https://forums.phpfreaks.com/topic/27930-convert-to-timestamp/#findComment-127757
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.