How to convert full date/time to timestamp?

What function in Python and PHP can convert time in specified time zone to Unix timestamp?

  • I have a lot of times in various time zones to process, but the mktime() function in both PHP and Python have no parameters about time zone, it just convert to timestamps in my system time zone. Is there any function accept a argument such as +32400 or -32400 for UTC+9?

  • Answer:

    In both of those libraries, there is a way to set the time zone used for mktime(). In PHP, it is date_default_timezone_set(). (http://us3.php.net/manual/en/function.date-default-timezone-set.php) In Python, it is setting the "TZ" environmental variable and then calling time.tzset(). (http://docs.python.org/library/time.html#time.tzset) There are other Python solutions, such as using the datetime library, or using the email.utils.mktime_tz() funciton. It might be easier, however, to simply add/subtract the proper number of seconds to adjust after you have already converted in UTC.

Xuan Luo at Quora Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.