How to change timezones and format time in PHP? - php

I have a variable like this,
$Timestamp = "Tue Mar 8 15:59:00 UTC-05:00 2016";
How do I change its format to YYYYMM-DD HH:MM AM/PM and change the timezone from UTC to Pacific Time using PHP?

PHP's DateTime object is pretty flexible.
$UTC = new DateTimeZone("UTC");
$TZ = new DateTimeZone("America/New_York");
$date = new DateTime( "2016-03-09 15:00:00", $UTC );
$date->setTimezone( $TZ );
echo $date->format('Y-m-d H:i:s');

Use php date: date('Y/m/d H:i', $timestamp) but your timestamp should be int:
$timestamp = strtotime('22-09-2008');

You can try following
$Timestamp = "Tue Mar 1 15:59:00 UTC-05:00 2016";
$datetime = new DateTime($Timestamp);
$datetime->format('Y-m-d H:i:s') . "\n";
$new_time = new DateTimeZone('Pacific/Apia');
$datetime->setTimezone($new_time);
//New formatted time
echo $datetime->format('Y-m-d H:i:s');

Related

Converting from UTC to any timezone

My linux box is set to use UTC. Given a timezone and a date, I want to get the date range so that I can query the database for records created on any given day. For instance, if it is now 2018-03-24 at 9am in America/Denver timezone. I want to get the start and end times for this date in UTC. How can I get the UTC equivilant of the beginning of that date?
<?php
$date = new DateTime(date('Y-m-d H:i:s'), new DateTimeZone('America/Denver'));
$date->setTimezone(new DateTimeZone('UTC'));
echo $date->format('Y-m-d 00:00:00');
?>
this returns 2018-03-24 00:00:00 which is not correct. Any pointers?
Try using this function.
function UTCTimeToLocalTime($time, $tz = '', $FromDateFormat = 'Y-m-d H:i:s', $ToDateFormat = 'Y-m-d H:i:s')
{
if ($tz == '')
$tz = date_default_timezone_get();
$utc_datetime = DateTime::createFromFormat($FromDateFormat, $time, new
DateTimeZone('UTC'));
$local_datetime = $utc_datetime;
$local_datetime->setTimeZone(new DateTimeZone($tz));
return $local_datetime->format($ToDateFormat);
}
echo UTCTimeToLocalTime('2015-07-01 13:30:00','America/Denver');
function LocalTimeToUTCTime($time, $tz = '', $FromDateFormat = 'Y-m-d H:i:s', $ToDateFormat = 'Y-m-d H:i:s')
{
if ($tz == '')
$tz = date_default_timezone_get();
$utc_datetime = DateTime::createFromFormat($FromDateFormat, $time, new
DateTimeZone($tz));
$local_datetime = $utc_datetime;
$local_datetime->setTimeZone(new DateTimeZone('UTC'));
return $local_datetime->format($ToDateFormat);
}
You feed the DateTime constructor with a bogus local time:
new DateTime(date('Y-m-d H:i:s'), newDateTimeZone('America/Denver'));
^^^^
You tell PHP that's a Denver local time but you don't really know. Since the string does not contain time zone information PHP will use the default time zone.
Just drop date(). It serves no purpose and only makes things harder.
<?php
$date = new DateTime('now', new DateTimeZone('America/Denver'));
echo $date->format('r'), PHP_EOL;
$date->setTimezone(new DateTimeZone('UTC'));
echo $date->format('r'), PHP_EOL;
Sun, 25 Mar 2018 06:11:21 -0600
Sun, 25 Mar 2018 12:11:21 +0000

DateTime 7 days from now at 00:00

I'm trying to get the date 1 week before at 00:00:00 Here's what I've tried
$now = date("Y-m-d H:i:s");
$start_date_time = date('Y-m-d H:i:s', strtotime("-7 day"));
Output is 2017-04-11 11:33:52 (UTC)
I tried to use
$start_date_time ->setTime(0, 0);
echo $start_date_time ->format('H:i:s');
But it's not datetime so I get an error. Any tips?
you could use strtotime
$start_date_time = date('Y-m-d', strtotime('-7 days'));
or DateTime class
$date = new DateTime('7 days ago');
$date ->format('Y-m-d');
Just use DateTime and DateInterval.
Example:
$date = new DateTime();
$date->sub(new \DateInterval('P7D'));
echo $date->format('Y-m-d 00:00:00');
$d=strtotime("-7 Days");
echo date("Y-m-d 00:00:00", $d);

Cannot convert string into DateTime

I'm trying to convert this date: 25/08/2016 10:45
into DateTime using this:
$time = strtotime('25/08/2016 10:45');
$newformat = date('Y-m-d H:i:s', $time);
but I get false from time, why?
Try this:
$dt = DateTime::createFromFormat('d/m/Y H:i', '25/08/2016 10:45');
echo $dt->format('Y-m-d H:i:s');

PHP convert mktime result to UTC

I am using this code to generate yesterday's beginning of day in PST (aka America/Los_Angeles) time. I can't figure out how to convert the result to UTC.
date_default_timezone_set("America/Los_Angeles");
$time1 = date("Y-m-d H:i:s", mktime(0,0,0, date('n'), date('j')-1, date('Y')));
I tried this, but $time1 is not datetime, it's string. So the following won't work.
$time1->setTimezone(new DateTimeZone("UTC"));
The DateTime class can do all that for you
$date = new DateTime(null, new DateTimeZone('America/Los_Angeles')); // will use now
echo $date->format('d/m/Y H:i:s'); //16/08/2016 16:13:29
$date->setTime(0,0,0);
$date->modify('-1 day');
echo $date->format('d/m/Y H:i:s'); // 15/08/2016 00:00:00
$date->setTimezone(new DateTimeZone('UTC'));
echo $date->format('d/m/Y H:i:s'); // 15/08/2016 07:00:00

error when echo date() format?

I have a datetime with format: 2012-03-30 11:47:58
$datetime = '2012-03-30 11:47:58';
$publishdate = date('m/d/Y H:i:s', $datetime );
When I echo $publishdate is result is '01/01/1970 07:33:32' , How to fix it
$publishdate = date('m/d/Y H:i:s', strtotime($datetime) );
Or using DateTime
$date = new DateTime($datetime);
$publishdate = $date->format('m/d/Y H:i:s');
http://php.net/manual/en/datetime.format.php
$datetime = '2012-03-30 11:47:58';
$timestamp = strtotime($datetime);
$publishdate = date('m/d/Y H:i:s', $timestamp);
First, convert your date and time to timestamp with strtotime() method, then format again with date() function.
Something like the following should work, note the call to strtotime(), it turns the datestring into a date so you can format it:
$datetime = '2012-03-30 11:47:58';
$publishdate = date('m/d/Y H:i:s', strtotime($datetime) );

Categories