PHP date_timezone_set stops date format from showing 24hr time - php

I have the following code:
$date = date_create('2014-11-06T23:00:00.000Z');
date_timezone_set($date, timezone_open('Australia/Victoria'));
echo $date ->format('Y-m-d H:i:s');
And it results in:
2014-11-07 10:00:00
However in the PHP doco here it says that capital H is 24hr time:
http://php.net/manual/en/function.date.php
H 24-hour format of an hour with leading zeros 00 through 23
If I comment out the middle line as follows:
$date = date_create('2014-11-06T23:00:00.000Z');
#date_timezone_set($date, timezone_open('Australia/Victoria'));
echo $date ->format('Y-m-d H:i:s');
It results in:
2014-11-06 23:00:00
Which does show 24hr time, but the date is a day out, which is why I need to set the timezone.
Why does setting the date_timezone_set stop me from being able to show the date in 24hr time format, and how to do fix this?

Unless you specify a timezone date_create assumes the current timezone. If you don't define one then it will use UTC.
Since the timezone is specified after the date_create the date is stored as UTC.
Since 2014-11-06 23:00 UTC is the same time as 2014-11-07 10:00 UTC+11 (Australia/Victoria) you will see the latter when you 'echo' the date.

Related

unix timestamp datetime difference php

I am running into a strange problem with a discrepancy between a unix timestamp and the converted result.
Here is an example:
$timestamp = 1489132800;
echo date('l jS \of F Y h:i:s A', $timestamp);
// echo statement = Friday 10th of March 2017 03:00:00 AM
// on unixtimestamp.com this equates to 03/10/2017 # 8:00am (UTC)
Does anyone have any idea why there would be a 5 hour difference? Does the date function rely on some internal time setting?
This is because your converter returns the time in UTC, while your local timezone is 5 hours behind UTC.
You can check your current timezone via date_default_timezone_get() and change it with date_default_timezone_set().
You can also check with some onlineconverters that return your local time as well as UTC time: http://www.convertunixdate.com/

PHP date format, AM and PM reversed

I am echoing out a date with the following code:
<?php echo date('h:i A', strtotime($catch[0]['catch_date'])); ?>
If I add it at 1:50PM it shows as: 1:50AM when echo'd out instead of showing PM.
I am stumped on this one. Any ideas?
Dates are being entered at the time of addition as:
'catch_date' => date('Y-m-d h:i:s')
You are using the wrong format.
From the manual:-
h 12-hour format of an hour with leading zeros 01 through 12
H 24-hour format of an hour with leading zeros 00 through 23
So you need to change your code to:-
'catch_date' => date('Y-m-d H:i:s')
You should set the default time zone before printing out the date. Example:
date_default_timezone_set('Pacific/Auckland');
And here is a full list of supported time zones:
https://www.php.net/manual/en/timezones.php

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.

Why is getTimestamp() affected by setTimezone()

I've been struggling quite a while with PHP's DateTime classes. My understanding is that a UNIX-timstamp is always in UTC, regardless of the timezone.
That's why I am quite confused with this code sample.
$date1 = new DateTime("#1351382400"); // Sun Oct 28 2012 02:00:00 GMT+2 (DST)
var_dump($date1->getTimestamp()); //prints: 1351382400
$date1->setTimezone(new DateTimeZone("Europe/Stockholm"););
var_dump($date1->getTimestamp()); //prints: 1351386000
As you can see setTimezone() changes the result of getTimestamp().
Is it expected that setTimezone() affects getTimestamp()?
The amount that you're off is 3600 seconds, or 1 hour.
I think that what you're seeing is because the date you picked is the end of Daylight Savings Time in Stockholm. If you use a different date, you don't get that effect:
$now = time();
echo " now: $now\n";
$date1 = new DateTime("#{$now}");
echo " date1 here: {$date1->getTimestamp()}\n";
$date1->setTimezone(new DateTimeZone("Europe/Stockholm"));
echo "date1 Stockholm: {$date1->getTimestamp()}\n";
Output:
now: 1352321491
date1 here: 1352321491
date1 Stockholm: 1352321491
I'm not sure if this is a bug or not, but it doesn't happen if you don't pick a date on which DST is changing.
Yes, unix timestamp is the current time as per the date object or your current machine time from Epoch.

PHP: Get Time for Current Region

I have a timezone of the user(he chooses it from a list)
I have a time in UTC(not current time)
So I need something like GetTimeForRegion(time, timezone) for PHP. Is there such functions or libraries or services?
you can use DateTime::setTimezone(). If your UTC date is an UNIX timestamp, you can use some code like this :
$date = new DateTime();
$date->setTimezone(new DateTimeZone('UTC'));
$date->setTimestamp(1297869844);
$date->setTimezone(new DateTimeZone('Europe/Paris'));
echo $date->format('Y-m-d H:i:s');
// Will print 2011-02-16 16:24:04
date('r') or date('c') may help you.
echo date('r') prints Thu, 16 Feb 2011 16:01:07 +0200
echo date('c') prints 2011-02-16T16:01:07+02:00
You need to look at the Date/Time API in PHP. I strongly advise you to stay away of gmdate and older date functions in php.
In your case, you should ask the user for its Olson based time zone.
The code of Artefact2 will do the trick.
please write this instead :
$date = date("Y-m-d H:i:s" , time());
so Y it means year m means month d means day
H get hours from 0 - 24
h get hours from 0 to 12
i get minutes
s get seconds

Categories