« Come on back Passive Voice | Main | CommonTimes Integrates with Del.icio.us and Technorati »

PHP Timestamp Hell - Converting ISO8601 timestamps

I hate messing with dates and times in all languages so I hope this helps someone speed along their day:

    function tstamptotime($tstamp) {
        // converts ISODATE to unix date
        // 1984-09-01T14:21:31Z
       sscanf($tstamp,"%u-%u-%uT%u:%u:%uZ",$year,$month,$day,
        $hour,$min,$sec);
        $newtstamp=mktime($hour,$min,$sec,$month,$day,$year);
        return $newtstamp;
    }                   

It converts an ISO8601 timestamp to a unix epoch (seconds past whenever unix started counting seconds).

Enjoy.

Comments

Todd

Yeah, I agree, fiddling with date conversions stinks. At least you didn't have to check your server to see whether it observes daylight savings time (only works on Linux, Windows barfs, btw).

Neil Wattam

Wow cheers, just what i was looking for. I also find all these time formats a royal pain in the back side, and a xml feed i use to keep a project of mine running desided to change from a unix time stamp to the ISO format, messing my entire project up...

All working again now...

Thanks

Andrew Somervell

Thank you Idealog + Google!

Olivier G.

Seems that strtotime() makes it also.

djodin

Thanks! Handy function.

Mike

This was really helpful, thanks.

jason

That was extremely useful! Thanks

Bradford

Thanks! This was very helpful.

Darren

That's saved me about 3 hours of pulling my hair out :) Thanks!

Post a comment

Comments are moderated, and will not appear on this weblog until the author has approved them.

If you have a TypeKey or TypePad account, please Sign In.