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
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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.