I have trouble with the following issue.
Environment: PHP 7.0.8
Code example:
$timestamp = -122615337600;
$dt1 = new \DateTime();
$dt1->setTimestamp($timestamp);
$dateStr = $dt1->format('Y-m-d H:i:s');
echo 'DateTime1: ' . $dateStr . '<br>';
$dt2 = new \DateTime($dateStr);
$dateStr = $dt2->format('Y-m-d H:i:s');
echo 'DateTime2: ' . $dateStr . '<br>';
I expect, that the output is equal. But I got:
DateTime1: -1916-06-22 00:00:00
DateTime2: -1916-06-21 00:00:00
Can someone tell me why is this and how can I fix it?
At first sight I would say, thats a PHP bug?
Related
How can I get the current date and time for this time zone 'America/phoenix'? I can't figure out how this can be done with the DateTime::createFromFormat.
My Code example
<?php
$format = 'n-j-Y g:iA';
$date = DateTime::createFromFormat($format, '/*Example: 6-12-2019 12:05am*/', new
DateTimeZone('America/phoenix'));
echo $date->format('n-j-Y g:iA') . "\n";
?>
You can change the second parameter of createFromFormat, but from your question, I guess you are not America/phoenix, therefore, use this code:
<?php
date_default_timezone_set('America/phoenix');
$currentTimeInPhoenix = date('n-j-Y g:iA');//gets the currenttime in phoenix
$format = 'n-j-Y g:iA';
$date = DateTime::createFromFormat($format, $currentTimeInPhoenix, new DateTimeZone('America/phoenix'));
echo $date->format('n-j-Y g:iA') . "\n";
date_default_timezone_set('YOUR OLD TIMEZONE');
?>
I have the following code but its not working as expected even though I am using a DateTime object as suggested by some posts and not to use strtotime()
<?php
$date = new DateTime('september 1st, 2016');
$now = new DateTime("now", new DateTimeZone('America/Phoenix') );
echo 'Date: '. $date->format('Y-m-d H:i:s') . '<br />';
echo 'Now: '. $now->format('Y-m-d H:i:s') . '<br />';
if($date < $now) {
echo '<b>Date is less than now</b>';
}
else
{
echo '<b>Date is greater than now</b>';
}
and the output I get is below, but how is it possible.
Date: 2016-09-01 00:00:00
Now: 2016-08-31 21:24:11
Date is less than now
Check these lines:
$date = new DateTime('september 1st, 2016'); // default timezone
$now = new DateTime("now", new DateTimeZone('America/Phoenix') ); // timezone is set
They both have different time zone of around 3 and half hours that's why the time is different.
In my postgresql, the I have the following column named "created" that has the type timestamp with timezone
So I inserted the record according to the format as such which I believe is UTC.
2015-10-02 09:09:35+08
I am using php Carbon library so i did the following:
$date = Carbon\Carbon::parse('2015-10-02 09:09:35+08');
echo $date->->toDatetimeString();
//gives result as 2015-10-02 09:09:35
How can I use the library to echo the correct timezone which includes the adding of the +8 in the above datetime format? The timzezone that I am using is "Asia/Singapore".
The time should be printed to local timing which is 2015-10-02: 17:09:35:
Try this:
$timestamp = '2015-10-02 16:34:00';
$date = Carbon::createFromFormat('Y-m-d H:i:s', $timestamp, 'Asia/Singapore');
Try this using standard PHP:
$raw = '2015-10-02 09:09:35+08';
$date = substr($raw,0,19);
$tzOffset = (strlen($raw) > 19) ? substr($raw,-3) : 0;
$timestamp = strtotime($date) + (60 * 60 * $tzOffset);
$localTime = date('Y-m-d H:i:s',$timestamp);
echo 'local time:['.$localTime.']';
The result is:
local time:[2015-10-02 17:09:35]
This will also work without a time zone offset or a negative one.
You can do this using native php without using Carbon:
$time = '2015-10-02 16:34:00+08';
$date = DateTime::createFromFormat('Y-m-d H:i:s+O', $time);
print $date->format('Y-m-d H:i:s') . PHP_EOL;
$date->setTimeZone(new DateTimeZone('Asia/Singapore'));
print $date->format('Y-m-d H:i:s') . PHP_EOL;
$date->setTimeZone(new DateTimeZone('Etc/UTC'));
print $date->format('Y-m-d H:i:s') . PHP_EOL;
I'm receiving some data from an HTTP POST which includes what is labelled a GMT Timestamp:
<gmt_timestamp>201308031525</gmt_timestamp>
I then need to take this timestamp and convert it to this format:
MM/DD/YYYY HH:MM
So far I've been trying this:
$ts = $_GET['timestamp'];
$date = DateTime::createFromFormat('ymdHi', $ts);
$fmTimestamp = $date->format('m/d/Y h:i:s A');
but that generates a "PHP Fatal error: Call to a member function format() on a non-object" for the 2nd line. Any idea what I'm doing wrong?
You have a bug in this line:
$date = DateTime::createFromFormat('ymdHi', $ts);
You need an uppercase Y for the year:
$date = DateTime::createFromFormat('YmdHi', $ts);
A lowercase y indicates "A two digit representation of a year", whereas you need Y ("A full numeric representation of a year, 4 digits"). See the docs here.
You also need to set the timezone before you begin:
date_default_timezone_set('UTC');
(PHP does have a GMT timezone, but it shouldn't be used. UTC behaves the same as GMT within PHP.)
Edit
To get your desired output format of:
MM/DD/YYYY HH:MM
you need to do:
$fmTimestamp = $date->format('m/d/Y H:i');
Also, since you're "receiving some data from an HTTP POST", you need to use $_POST instead of $_GET:
$ts = $_POST['timestamp'];
So the complete code is:
date_default_timezone_set('UTC');
$ts = $_POST['timestamp'];
$date = DateTime::createFromFormat('YmdHi', $ts);
$fmTimestamp = $date->format('m/d/Y H:i');
Keep it simple stupid.
$input = $_GET['timestamp']; // 201308031525
$year = (int)substr($input,0,4);
$month = (int)substr($input,4,2);
$date = (int)substr($input,6,2);
$hour = (int)substr($input,8,2);
$minute = (int)substr($input,10);
$date_obj = new DateTime($year . '-' . $month . '-' . $date .' ' . $hour . ':' . $minute);
echo $date_obj->format('m/d/Y h:i:s A');
and the output is:
08/03/2013 03:25:00 PM
You are not instantiating the object you are trying to use.
Try this approach instead:
$date = new DateTime;
$date->createFromFormat('ymdHi', $ts);
$fmTimestamp = $date->format('m/d/Y h:i:s A');
This is untested, just saying ...
i have a time zone in 2010-05-04T05:27:00.000Z format which indicates the GMT time and i want to add GMT 10+ in to it using php.
i can do that thing using following code but how would i directly add 2010-05-04T05:27:00.000Z and GMT 10+ so that i can get a valid date and time.
$offset=10*60*60;
$dateFormat="d-m-Y H:i::m:s";
echo $timeNdate=gmdate($dateFormat, time()+$offset);
Maybe I'm missing the point but are you not really looking for DateTime::setTimezone?
$timezone = new DateTimeZone('Etc/GMT-10'); // GMT+10:00
$datetime = new DateTime('2010-05-04T05:27:00.000Z');
$datetime->setTimezone($timezone);
echo $datetime->format('r');
// Tue, 04 May 2010 15:27:00 +1000
Use DateTime class http://php.net/manual/en/book.datetime.php exacly DateTime::Add() http://www.php.net/manual/en/datetime.add.php
You have some example here:
<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('PT10H30S'));
echo $date->format('Y-m-d H:i:s') . "\n";
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P7Y5M4DT4H3M2S'));
echo $date->format('Y-m-d H:i:s') . "\n";
?>
And another:
<?php
$date = new DateTime('2000-12-31');
$interval = new DateInterval('P1M');
$date->add($interval);
echo $date->format('Y-m-d') . "\n";
$date->add($interval);
echo $date->format('Y-m-d') . "\n";
?>