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");
Related
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 );
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
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.
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.
I am trying to create a select list starting from the current date of the user. I want it so that it is set to midnight in unix timestamp format.
This is all I'm doing:
$today = strtotime('today');
echo $today;
This is my result:
1333144800
which is: Fri, 30 Mar 2012 22:00:00 GMT according to Epoch Converter (incorrect by a couple hours.)
If you want strtotime() to return a timestamp relative to UTC (00:00:00 UTC instead of e.g. 00:00:00 UTC+2, if your system is set to a timezone with an offset of 2 hours against UTC/GMT), you need to specify that:
$today = strtotime('today UTC');
GMT (+0) time
echo $today = strtotime('today GMT');
echo "<br>" . $today = date("d-m-Y H:i:s", $today);
We expect that your server runs at GMT - that is the best (for maneuvering with time displays later). If not, you MUST adjust php.ini set this "date.timezone = GMT".
When you get that done, you will see 00:00 with my codes.
Then, you must develop function (ie DisplayDate()) in your script to display dates of your website correctly if
youre not in GMT area
or/and if you want for your users to see times in their timezone with timezone selection for example.
DisplayDate() should include support for daylight changes also (0, or +1 hour / summer and winter time).
strtotime( $time )
is designed to return a unix timetamp, meaning, it will return the number of seconds since jan 1, 1970. http://www.php.net/manual/en/function.strtotime.php
To get around this, use something like:
$today = date("d/m/Y H:i:s", strtotime('today'));
echo $today;
You might have to specifically set the time as well as the day:
$today_midnight = strtotime('today UTC 00:00');
You should check the timezone configuration in your php.ini file. In my case (I live in El Salvador) I had to change it like this:
date.timezone = America/El_Salvador