Unable to get last friday date in PST time in PHP - php

I am trying to getting last friday date PST time, i am getting 15-11-2018,
Here is my code,
public function getPSTCurrentTime($time=null) {
$dateTime = new \DateTime($time, (new \DateTimeZone('UTC'))); // get current time as UTC/GMT timezone
$dateTime->setTimezone(new \DateTimeZone('PST')); // convert time as PST timezone
return $dateTime;
}
$date = new getPSTCurrentTime('last friday')->format('Y-m-d h:i:s');
The output i am getting was 15-11-2018, But i am expecting output was 16-11-2018

Try this,
$pst = new DateTimeZone('America/Los_Angeles');
$last_friday = new DateTime('last friday', $pst);
echo $last_friday->format('Y-m-d H:i:s'); // "2018-11-16 00:00:00"
After Comment:
To get the date based on the current timezone, then use date_default_timezone_get
$current_timezone = new DateTimeZone(date_default_timezone_get());
$last_friday = new DateTime('last friday', $current_timezone);
Output:
2018-11-16 00:00:00

Related

how to set 00:00:00 for DateTime

What I want to get are
today's 00:00:00
week start day's 00:00:00
month start day's 00:00:00
year start day's 00:00:00
So I write the code,
$today = new \DateTime();
$weekStart = new \DateTime();
$monthStart = new \DateTime();
$yearStart = new \DateTime();
$weekStart->modify('last sunday');
$monthStart->modify('first day of this months');
$yearStart->modify('first day of this year');
print $today->format('Y-m-d h:i:s')."\n";
print $weekStart->format('Y-m-d h:i:s')."\n";
print $monthStart->format('Y-m-d h:i:s')."\n";
print $yearStart->format('Y-m-d h:i:s')."\n";
It gave me the result:
2018-11-13 09:34:02
2018-11-11 12:00:00
2018-11-01 09:34:02
2018-11-01 09:34:02
I have two questions.
How can I make each DateTime 00:00:00??
How can I get the first day of year???
PHP's date parsing is somewhat tricky. Here's the code snippet which does what you want:
$today = new \DateTime('midnight');
$weekStart = new \DateTime('midnight last sunday');
$monthStart = new \DateTime('midnight first day of this month');
$yearStart = new \DateTime('midnight first day of january this year');
echo $today->format('Y-m-d H:i:s')."\n";
echo $weekStart->format('Y-m-d H:i:s')."\n";
echo $monthStart->format('Y-m-d H:i:s')."\n";
echo $yearStart->format('Y-m-d H:i:s')."\n";
DateTime value can be set upon construction; there's no need to call DateTime::modify().
Add midnight to get the time set to 00:00:00.
first day this year doesn't work in PHP, you need to use first day of january this year instead.
Reference: PHP Documentation of relative date formats.
you can try:
$date->setTime(0, 0, 0);
$date = new DateTime('2012-07-22');
echo $date->format('Y-m-d 00:00:00');
NO need to create a object of datetime(), use above code to get current date with time 00:00:00

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

Format a time with timezone to local time in PHP

How can a change times that are provided with a timezone (eg. 14:00:00+02:00) to a local time (the result would be 16:00:00) when the dates are not provided.
I've tried it with the DateTime class. But since I'm only handling times, not dates, it doesn't always provide the correct results:
$time = '14:00:00+02:00';
$datetime = new DateTime($date);
$result = $datetime->format("H:i:s");
Use DateTime's setTimezone method. It will automatically change the time to match the timezone.
$date = new DateTo,e("2017-02-02 10:00:00", new DateTimeZone("+0200"));
$date->setTimezone(new DateTimeZone("-0200"));
var_dump($date->format("H:i:s"));
Will output the following:
string '06:00:00' (length=8)
$utc = new DateTimeZone("UTC");
$zone = new DateTimeZone("YOUR_TIME_ZONE");
$date = new DateTime("14:00:00+02:00", $utc);
$date->setTimezone($zone);
echo $date->format('Y-m-d H:i:s');
You can use setTimezone() function
$time = '14:00:00+02:00';
$datetime = new DateTime($time);
$result = $datetime->format("H:i:s");
echo $result;
echo '<br>';
$datetime->setTimezone(new DateTimeZone('Your_local_timezone')); // ex: UTC (+00h00) or Europe/Moscow (+4h00)
$result = $datetime->format("H:i:s");
echo $result;
You should specific your time zone, and tell DateTime to use Time zones
for example:
$time = '14:00:00+02:00';
$datetime = DateTime::createFromFormat('H:i:s+T', $time);
$datetime->setTimezone(new DateTimeZone('Europe/Warsaw'));
$result = $datetime->format("H:i:s");
echo($result);
Use +T in format pattern to say php thats date contain a Time zone, in next step u should set your timezone using setTimezone. For example i set 'Europe/Warsaw' but nothing happen coz +02:00 is CEST thats equal to 'Europe/Warsaw'
Other way is changing +02:00 to naming time zone here is a example:
$time = '14:00:00 (UTC)';
$datetime = new DateTime($time);
$datetime->setTimezone(new DateTimeZone('Europe/Warsaw'));
$result = $datetime->format("H:i:s");
echo($result);

PHP timezone issuse with Pacific/Auckland - Getting different value

$user_timezone_from = 'UTC';
$user_timezone_to = 'Pacific/Auckland';
$date = new DateTime('2015-08-21 14:00', new DateTimeZone($user_timezone_from));
$date->setTimezone(new DateTimeZone($user_timezone_to));
echo $cur_gmt_date = $date->format('Y-m-d H:i:s');
$date = new DateTime('2015-09-29 14:00', new DateTimeZone($user_timezone_from));
$date->setTimezone(new DateTimeZone($user_timezone_to));
echo $cur_gmt_date = $date->format('Y-m-d H:i:s');
I am getting different results for this.
2015-08-22 02:00:00
2015-09-30 03:00:00 It should be 2015-09-30 02:00:00
Why I got wrong value?
Wikipedia tells me that on the last Sunday of September there's a DST change in New Zealand. Hence a one hour difference to Summer Time.

how to convert php date formats to GMT and vice versa?

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;
}

Categories