Shifting unix time stamp according to timezone - php

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.

Related

Convert GMT date stamp to my timezone in PHP

I'm not sure what I'm doing is even correct. I'm being given a CSV export of transactions and the date stamp looks like a GMT timestamp to me, but when I try to convert it to my timezone and echo the time I just get the same timestamp.
$date = new DateTime('2019-11-12 13:43:12 +1300');
$date->setTimeZone(new DateTimeZone('Pacific/Auckland'));
echo $date->format("Y-m-d H:i:s");
The Timezone "Pacific/Auckland" with Daylight Saving Time (DST) is 13 hours ahead of GMT so if you take a date time string with an offset of +13:00 and set the DateTimeZone to "Pacific/Auckland" you won't see any difference of the time because the offset will be the same. You will only see a difference if you either have a UTC (ending with Z) or GMT date time string (ending with +00:00) and converting this to your time zone:
$date = new DateTime('2019-11-12 13:43:12+00:00');
$date->setTimeZone(new DateTimeZone('Pacific/Auckland'));
echo $date->format("Y-m-d H:i:sP");

how to convert unix timestamp to cet time in 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 );

Is there a referentially transparent way to work with times in php

I am trying to convert times to and from the following timezones, regardless of when or where the code is run:
The timezone of the running code
AEST (Australian Eastern Standard Time)
EDT (New York Eastern Daylight Time)
For example, given a unix timestamp, how do I find a new unix timestamp "monday the same week" using EDT timezone? How do I do this, such that it will always give the same result?
First you have to understand that a Unix Timestamp has nothing to do with timezones, it is always relative to UTC/GMT. To quote from the manual
Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
Now that you know one timestamp represents a fixed time in reference to GMT/UTC you can go ahead and change time zones in your code to calculate time for them form the same timestamp.
Let us say you have a unix timestamp
$ts = 1171502725;
If you create a date from it you would do something like
$date = new DateTime("#$ts");
echo $date->format('U = Y-m-d H:i:s T') . "\n";
Now you want to see what does that correspond to in EST, you can do
$date->setTimezone(new DateTimeZone('America/New_York'));
echo $date->format('U = Y-m-d H:i:s T') . "\n";
Similarly for CST
$date->setTimezone(new DateTimeZone('America/Chicago'));
echo $date->format('U = Y-m-d H:i:s T') . "\n";
And so on :)
Output
1171502725 = 2007-02-15 01:25:25 GMT+0000
1171502725 = 2007-02-14 20:25:25 EST
1171502725 = 2007-02-14 19:25:25 CST
Fiddle
You can get a list of supported timezones and their identifiers from the PHP Manual

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.

Datetime php conversions and usage with timezone

I am trying convert a utc time stored date to another time zone but i cant seem to get it right.
I have a time :
date1 = new DateTime('first day of the month');
date1.setTime(0,0,0); // Since using the first day of the month seems return the current time with different date
The default DateTime timezone is in UTC. The time i want to make reference is in 'Europe/Amsterdam' timezone. Any way i cant get the time in 'Europe/Amsterdam' timezone to be equivalent to the first day of the month time in UTC? (Uh, sorry my question was confusing.. let me just give an example to be clear). Im trying to query from a db.
If UTC date time is June 01, 2013. 00:00:00
I want to get get May 29, 2013 19:55:00.
I tried getting the difference between the two declared times with different timezones to get the time that i wanted but it seems it didnt work :(
My Edit/ Clarification:
If use this code:
$date1 = new DateTime('first day of the month');
$date1.setTime(0,0,0);
print_r($date1->format('Y-m-d H:i:s'));
I would get:
2013-06-01 00:00:00
Then if i use timezone:
$date1->setTimeZone(new DateTimeZone('Europe/Amsterdame'));
print_r($date1->format('Y-m-d H:i:s'));
I would get: (This is just a sample output):
2013-06-01 03:00:00
Because of time difference. Want i want to get is like the reverse: I want to get the datetime that when converted 'UTC' timezone i would get this: 06-01-2013 00:00:00 time. So my preffered output is : 2013-05-29 21:00:00 ...
You can do in an OOP way like so.
$date = new DateTime('2000-01-01 00:00:00', new DateTimeZone('Europe/Amsterdam'));
echo $date->format('Y-m-d H:i:s P') . "\n";
To set the default date in PHP, you can either set it in your ini file or in a PHP file like so:
date_default_timezone_set('Europe/Amsterdam');
Then to format the date, refer to http://www.php.net/manual/en/function.date.php for formatting.
In your case this would be:
date('j M Y' time());
Where j = day, M = month and Y = year.

Categories