2

I have stored a time in my database, for example this data: 2014-03-25 13:15:00

But when I use UNIX_TIMESTAMP(date_field) through mysql query or strtotime() in PHP, both functions output is effected by timezone.

Please tell me if it's possible to get unix time without timezone effect? It will also be very helpful if anyone can provide a PHP function.

Please do not suggest to me to change the timezone, because this is not a suitable solution.

Current output of 2014-03-25 14:05:00 when covert to unix and then again convert to date 2014-03-25 08:35:00

UPDATE

this not only happen only to DB time. like if I directly call this : date('Y-m-d H:i:s', strtotime(2014-03-25 14:05:00))

then it output

2014-03-25 08:35:00
12
  • What timezone are your servers set to? Commented Mar 25, 2014 at 8:08
  • 1
    what data type you used to store the time? Commented Mar 25, 2014 at 8:09
  • I never set timezone, i think it will be default timezone. (by the way I am using WAMP) Commented Mar 25, 2014 at 8:09
  • 1
    @hago I use timestamp data type, but this problem is related to timezone Commented Mar 25, 2014 at 8:10
  • 2
    Performing UNIX_TIMESTAMP(timestamp_type_column) will not perform any timezone conversion whatsoever, as TIMESTAMP type columns are already stored in UTC. However, storing literal '2014-03-25 13:15:00' into a TIMESTAMP type column must therefore convert from the session's time_zone to UTC, which is presumably the effect to which your question refers. But how could one expect otherwise? Commented Mar 25, 2014 at 8:21

1 Answer 1

1

With PHP you can use DateTime:

$date = new DateTime('2014-03-25 13:15:00', new DateTimeZone('UTC'));

echo $date->format('Y-m-d H:i:s'); // 2014-03-25 13:15:00
echo $date->format('U'); // 1395753300

This assumes the times in your database are expressed in UTC.

Before PHP 5.3

$date = '2014-03-25 13:15:00';
echo gmdate('Y-m-d H:i:s', strtotime("$date GMT"));
Sign up to request clarification or add additional context in comments.

8 Comments

Of course, this assumes that the literal is in fact in UTC (which I somehow doubt). :)
Thanks I will try this solution, but most of my users are not uing php5.3 (i know its old) but then I have to ask them to update to 5.3. But indeed I will try this.
@user007 I've added pre 5.3 version.
Your solution output correct date when I use gmdate() but still UNIX time is not correct. I have already time stamp, and I want correct UNIX time. Thanks for your help.
strtotime('2014-03-25 14:05:00') is outputting 1395756300
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.