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
Related
Hello I have a string 2020-08-19 13:04:53. I know that this time is in Sydney, Australia time. However, I need to convert this time into New York, America time as a date object. How can I do this?
date_default_timezone_set("Australia/Sydney");
$time = '2020-08-19 13:04:53';
$date = date('Y-m-d H:i:s', $time);
I would try something like
<?php
$date = new DateTime('2020-08-19 13:04:53', new DateTimeZone('Australia/Sydney'));
echo $date->format('Y-m-d H:i:s');
// Output: 2020-08-19 13:04:53
$date->setTimezone(new DateTimeZone('America/New_York'));
echo $date->format('Y-m-d H:i:s');
// Output: 2020-08-18 23:04:53
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);
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');
I am sure the answer is right in front of me, but I have a UTC dateTime that looks like this:
2014-01-02 04:02:58
All I want to do is add some hours to that.
How can I achieve this in PHP? Further, how would I add days to it if I wanted to?
Use DateTime(). Unlike date()/strtotime() it is timezone and daylight savings time friendly.
// PHP 5.2+
$dt = new DateTime('2014-01-02 04:02:58');
$dt->modify('+2 hours');
echo $dt->format('Y-m-d H:i:s');
$dt->modify('+2 days');
echo $dt->format('Y-m-d H:i:s');
See it in action
Or
// PHP 5.3+
$dt = new DateTime('2014-01-02 04:02:58');
$dt->add(new DateInterval('PT2H'));
echo $dt->format('Y-m-d H:i:s');
$dt->add(new DateInterval('P2D'));
echo $dt->format('Y-m-d H:i:s');
See it in action
Or
// PHP 5.4+
echo (new DateTime('2014-01-02 04:02:58'))->add(new DateInterval('PT2H'))->format('Y-m-d H:i:s');
See it in action
Reference:
DateTime()
DateInterval()
You can use strtotime() try like this :
echo $new_time = date("Y-m-d H:i:s", strtotime( "2014-01-02 04:02:58".'+3 hours'));
i am new to php.
i want to write a function where i need user to input date in any date format including DST,into GMT format and again later back into the original entered format.please any body help me.
Although the gmdate functions are available. If you are using PHP 5.2 or greater, then consider using the DateTime object.
Here's code to switch to GMT
$date = new DateTime();
$date->setTimezone(new DateTimeZone('GMT'));
and back to the default timezone...
$date = new DateTime('2011-01-01', new DateTimeZone('GMT'));
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
Using the DateTime object lets your create a datetime, just like the procedural functions, except that you keep a reference to an instance.
e.g.
// Get a reference to Christmas of 2011, at lunch time.
$date = new DateTime('2011-12-25 13:00:00');
// Print the date for people to see, in whatever format we specify.
echo $date->format('D jS M y');
// Change the timezone to GMT.
$date->setTimezone(new DateTimeZone('GMT'));
// Now print the date/time it would in the GMT timezone
// as opposed to the default timezone it was created with.
echo $date->format('Y-m-d H:i:s');
// Just to show of some more, get the previous Sunday
$date->modify('previous Sunday');
There's a whole lot of functions you can use, that are much more readable that the procedural functions.
Explicit example of converting from a timezone to GMT
$melbourne = new DateTimeZone('Australia/Melbourne');
$gmt = new DateTimeZone('GMT');
$date = new DateTime('2011-12-25 00:00:00', $melbourne);
$date->setTimezone($gmt);
echo $date->format('Y-m-d H:i:s');
// Output: 2011-12-24 13:00:00
// At midnight on Christmas eve in Melbourne it will be 1pm on Christmas Eve GMT.
echo '<br/>';
// Convert it back to Australia/Melbourne
$date->setTimezone($melbourne);
echo $date->format('Y-m-d H:i:s');
Using your Asia/Kolkata to America/New_York
date_default_timezone_set('Asia/Kolkata');
$date = new DateTime('2011-03-28 13:00:00');
$date->setTimezone(new DateTimeZone('America/New_York'));
echo $date->format("Y-m-d H:i:s");
//Outputs: 2011-03-28 03:30:00
Use the gmdate function to convert to GMT time.
For example
$d = '2011-03-28 12:05:20';
$gmt = gmdate('Y-m-d H:i:s',strtotime($d));
// Convert local time to gmt
public function convertTime($timezone,$time){
$selectedtime = date("Y-m-d H:i",strtotime($time));
$date = new DateTime($selectedtime, new DateTimeZone($timezone));
$date->setTimezone(new DateTimeZone('GMT'));
$convertedtime = strtotime($date->format('Y-m-d H:i'));
return $convertedtime;
}