how to convert unix timestamp to cet time in php? - php

I have a simple question for you. I have a unix timestamp variable in php, let me call it $unix, which contains the current unix timestamp (seconds passed after 1/1/1970 00:00:00 UTC), that by definition is UTC time. I want to convert it to a date string in CET time, like "29/03/19 14:50". It is crucial to have the CET format for me, not changing when the daylight saving time (DST) begins. In other words, the date I want is UTC+1 out DST, and during DST, and this has to be done automatically.
I was thinking about using this function:
date_create_from_format ( string $format , string $time [, DateTimeZone $timezone ] )
so I should write something like:
date_create_from_format ('d/m/y h:i' , $unix, 'CET');
Am I wrong? What does not convince me is that in this page it is clarely written that:
The timezone parameter and the current timezone are ignored when the time parameter either contains a UNIX timestamp (e.g. 946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00).
Is there a way to do what I want?
Thank you!

First convert your date time in php data time format...
gmdate("Y-m-d\TH:i:s\Z", $timestamp);
Now you can display PHP date time from UTC time to specific timezone.
$date_time_format = $datetime->format('Y-m-d H:i:s');
$time_zone_from="UTC";
$time_zone_to='Asia/Kolkata';
$display_date = new DateTime($date_time_format, new DateTimeZone($time_zone_from));
$display_date->setTimezone(new DateTimeZone($time_zone_to));
echo $display_date->format('d-m-Y H:i:s')

This should bring your desired result:
date_default_timezone_set( 'CET' );
echo date('d/m/y h:i', $unix );

Related

ISO date formats to wrong day

Short question but I can't get my finger on it. This piece of code:
$date = '2015-12-08T00:00:00+01:00';
echo date('D', strtotime($date));
returns Mon while
$date = '2015-12-08T00:00:00';
echo date('D', strtotime($date));
returns Tue. Why is that? The +01:00 is for the timezone, but that should not affect the day in my opinion.
First I've looked up that 08-12-2015 is in fact a Tuesday, so now we know the first one is incorrect.
PHP's date() is an Unix timestamp according to their own docs.
My belief is that adding the +1 as a timezone triggers the calculation to the +0 timezone (UTC) when asking for the day of the week and therefore returns 23:00 on monday as the current UTC time.
You can specify the timezone before executing the rest of the code: date_default_timezone_set('Europe/Amsterdam');
<?php
date_default_timezone_set('Europe/Amsterdam'); //this is an example of a +1 timezone, choose one from http://php.net/manual/en/timezones.php
$date = '2015-12-08T00:00:00+01:00';
echo date('D', strtotime($date) );
?>
strtotime will parse your date string using the supplied time zone or using the default timezone if unspecified. We can't see from the code you've posted what time zone your server is configured to, but once the date is parsed and converted to your time zone, the time may legitimately occur in the previous day, hence why you're seeing 'Mon'.
Either supply a time zone in the strtotime call via the now argument or set one globally with date_default_timezone_set.

PHP date timestamp timezone not converted properly

So I have this code:
$timestamp = 1414708099;
echo $timestamp;
$date = date_make_date($timestamp, 'UTC', 'datestamp');
date_timezone_set($date, timezone_open('America/New_York'));
$timestamp = $date->format('U');
echo '<br>';
echo $timestamp;
which is supposed to convert the timezone of the initial timestamp from UTC to new york.
but then this ends up printing
1414708099<br>1414708099
hence the timezone didnt change...
what did I do wrong?
btw it also uses Drupal 6 date_api.module: http://drupalcontrib.org/api/drupal/contributions!date!date_api.module/function/date_make_date/6
As per comments
A timestamp is always UTC. You can't apply a time zone to a timestamp - consider its timezone as 0. Whatever you do, it stays 0. You asked for a date formatted with U - manual states this:
U: Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
You can't get seconds from Unix Epoch for New York. That number is the same for any location in the world.
Now, had you formatted that date using, say, $date->format('Y-m-d H:i:s') then you would get correctly formatted time with the timezone offset for New York.
Long story short - there is no problem whatsoever here. It all works as intended.

Shifting unix time stamp according to timezone

I know this question has been answered many many times. I came in to a solution to solve this and its goes like this. I store all time stamps for each post in UTC on the server. Now i need to display the time stamp for a given timezone. I do this:
$tz : requested timezone
$ts : timstamp on db
$newts : new timestamp
$datetime = date('m/d/Y g:i a', $ts);
$dt = new DateTime($datetime, new DateTimeZone('UTC'));
date_default_timezone_set(trim($tz));
$newts = $dt->format('U');
date_default_timezone_set('UTC');
However the resulting time stamp is 60~ seconds higher than what is should be.
What am i doing wrong?
You're close, all you need to do is create the original DateTime object based on the timestamp/server timezone then set the new timezone and print the result, like so:
$datetime = new DateTime('#'.$ts, new DateTimeZone('UTC'));
$datetime->setTimezone(new DateTimeZone($tz));
print $datetime->format('m/d/Y g:i a');
The unix timestamp will be the same regardless of the timezone (it is TZ agnostic). The offset occurs when displaying it for different time zones. This is you can test this by printing the unix timestamp for each different timezone (they will be the same).
The U format gives you the number of "seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)". That number does not depend on where on earth you are. So even if it might be 14:59 at your place while it's 10:24 at my place, the number of seconds since January 1 1970 00:00:00 GMT is the same at both our places. A "time stamp for a given timezone" does not make sense.

Date / Time showing odd value in PHP

I have wierd issues with time / date in PHP this year. Code have not changed at all and my dates are bugged.
Code is for example:
$date = strtotime($order['date']);
$dateNew = date('Y-m-d h:i A', $date);
print $dateNew;
Returns 1969-12-31 07:00 PM for some reasson, altough:
print $order['date'];
Returns 2013-01-12 18:25:43
I'm confused because I'm quite sure that my code is correct.
I dare you to solve this bugger!
The function strtotime() was made for transform English into date format.
The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.
As i don't know what is really into your $order variable i will suggest 2 solutions :
Maybe you can avoid the strtotime function and replace it by date() directly like this :
$order = ['date' => '2013-01-12 18:25:43'];
$date = date($order['date']);
It works well here: http://codepad.viper-7.com/cbNA87
Or, if it's not working consider to use mktime(), it will convert the date into seconds since the epoch.
The Unix epoch is the reference point for all time stamps. PHP calculates the times from this date in seconds.
The $date should be null and your server in the east coast of the US so it's returns the epoch :)
PHP returns the date 1969-12-31 when there is not a proper date. So if you did
$date = 0;
$dateNew = date('Y-m-d', strtotime($date));
Your result would be 1969-12-31, since that is the default Unix epoch time. http://php.net/manual/en/function.time.php
Unexpected dates of "1969-12-31 07:00 PM" means something went wrong with date() .
your strototime($order['date']) is probably returning false (failing to parse it to a unix timestamp).
Try this and ensure its returning an int (not false)
var_dump($order['date'], strtotime($order['date']));
See the error state of date: http://php.net/date
See the return values of strtotime: http://php.net/strtotime

Given an UTC offset and a date time, how can we find its UTC date time in PHP?

For example, I have a date time in this format and its UTC offset is +3:
2011-02-23 05:00:00
So its UTC time is actually:
2011-02-23 02:00:00
But what is the simplest way to do this conversion in PHP?
I have thought of using the DateTimeZone class but as you can see, I only have the offset but not the timezone name. And to construct a DateTimeZone object, the timezone name is required.
Maybe there are some better ways without using DateTimeZone class.
Thanks in advance.
Could just modify the date based on the offset. If you are interested in changing this to other timezones then create the date with the UTC timezone.
$date = new DateTime('2011-02-23 05:00:00', new DateTimeZone('UTC'));
$date->modify(sprintf('%s%d hours', $offset < 0 ? '+' : '-', $offset));
Note: This will work even if the offset is a string. i.e. "-3" or "+3"
Then for example if you wanted to see that time in Melbourne, Australia.
$date->setTimezone(new DateTimeZone('Australia/Melbourne'));
echo $date->format('c');
You can use date() and strtotime() for this.
$in = "2011-02-23 05:00:00";
$offset = "-3 hours";
$out = date('Y-m-d H:i:s',strtotime("$in $offset"));
It's also good to note that with this, you aren't restricted on what you're offsetting. The strtotime() function lets you do all kinds of wonderful things, like "-2 weeks +3 days ...", etc.
You are also free to get any date format from date(), or just use strtotime() alone to get the timestamp.

Categories