Jump to content

explode a date and convert to timestamp


chaking

Recommended Posts

So after thinking everything was working perfectly, I just noticed that a script that uses the function `strtotime` to convert a string (m/d/Y g:i:s A) into a unix timestamp doesn't actually return the correct seconds... instead it just leaves it at the closest minute (m/d/Y g:i A). 

 

I need a way to grab a date formatted as m/d/Y g:i:s A and convert it to the exact unix timestamp.

 

I believe using mktime could work, however I'm not sure how to break up the date so that I can put each individual piece where it's supposed to go in the mktime function:

 

So based on the format `m/d/Y g:i:s A` it will need to end up as mktime(g, i, s, m, d, Y)

 

It would be great to get some help on how to break up the string to fit it into the mktime, but there's also a pretty big issue and that's the `A` at the end.  It's AM or PM, so any ideas on how to convert that correctly would be appreciated too...

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/184824-explode-a-date-and-convert-to-timestamp/
Share on other sites

The answer is in the sql statement... so instead of grabbing the string from the datetime field, it can be converted to unix timestamp before outputting the data as so:

 

SELECT stamp = DATEDIFF(s, '19700101', some_column) FROM EVENTS WHERE some_column > 'xxx'

 

And that solves that - w00t

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.