开发者

Generate start and ending datetimes in UTC based on a PST date

I am taking in a parameter that is a date in PST timezone which will be in the format of "YYYY-MM-DD" (e.g. "2011-08-15"). This parameter is optional. I have 2 questions that I've been struggling with.

I need to calculate the start and end datetime in UTC for this date.

So if the inputted date is 2011-08-15, I want to 开发者_JS百科get the start and end datetimes:

2011-08-15 07:00:00

2011-08-15 06:59:59

(These are essentially the beginning and end of day)

Second, is to handle the case when the date is not passed in. I want to default to the current PST date them and start from there. So if the current datetime is 2011-08-01 10:00:00, I want to get the same start and end datetimes similar to the first scenario except it's based on the inputted date.

2011-08-01 07:00:00

2011-08-01 06:59:59

I've been pulling my hair out dealing with date and datetime conversions. I'm sure I'm missing something super straightforward.


Parse the date and assume PST timezone:

$date = new DateTime("2011-08-15", new DateTimeZone("PST"));

Change the timezone to UTC: (this does the all conversions for you)

$date->setTimeZone(new DateTimeZone("UTC"));

Calculate start and end. Start is our $date and end is $date + 1 day

$start = $date;
$end = clone $date;
$end->modify("+1 day"); // now $end is $start + 1 day

Print start/end:

printf("start: %s, end: %s\n", $start->format('Y-m-d H:i:s'), $end->format('Y-m-d Hi:s'));

// this prints start: 2011-08-15 07:00:00, end: 2011-08-16 07:00:00

For the last part of your questions, you can easily compare two dates:

if ($date > new DateTime()) { // if $date is after now
     // do something
}

So you could do something like that:

if ($date > new DateTime()) { 
    $date->setTimeZone(new DateTimeZone("UTC"));
}

If you don't much like the OO syntax you could also use the function aliases:

$date = date_create(...);
date_format($date, ...);
date_modify($date, ...);
// ...


Use setTimezone function

Working example (I'm not in PST timezone, so I have to set it explicitly)

$date_input = "20011-09-15";
//$date_input = null; //That will emulate no-input case
date_default_timezone_set("America/Los_Angeles"); //if you are in PST, you don't need this line
$date_start = new DateTime($date_input);
$date_end = new DateTime($date_input);
$date_end->modify("+1 day"); 

/*$date_start->setTimezone(new DateTimeZone("America/Los_Angeles"));
$date_end->setTimezone(new DateTimeZone("America/Los_Angeles"));*/

//end of date is equal to start of the next day.
//But, if you need something like 2011-08-11 23:59:59 add $date_end->modify('-1 second')
$date_start->setTime(0,0,0);
$date_end->setTime(0,0,0);

echo "Date Start PST:".$date_start->format("Y-m-d H:i:s")."<br/>";
echo "Date End PST:".$date_end->format("Y-m-d H:i:s")."<br/>";

//UTC is equal to London time. Almost :)
$date_start->setTimezone(new DateTimeZone ('Europe/London'));
$date_end->setTimezone(new DateTimeZone ('Europe/London'));

echo "Date Start UTC:".$date_start->format("Y-m-d H:i:s")."<br/>";
echo "Date End UTC:".$date_end->format("Y-m-d H:i:s")."<br/>";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜