Wednesday 15 July 2015

Adding 1 day to timestamp in PHP -



Adding 1 day to timestamp in PHP -

i have web app stores dates utc time stamps. dates changed display purposes clients using timezone setting. however, i've nail border case lastly sunday (2 nov 2014) when dst ended in usa. thought code handled case because uses strtotime , "+1 day" doesn't. here code i've got:

$current_date_start=$this->date_start; //$this->date_start utc timestamp $current_date_end=strtotime('+1 day', $current_date_start); { $current_date_start=strtotime('+1 day', $current_date_start); $current_date_end=strtotime('+1 day', $current_date_start); echo format_local_date($current_date_start,'america/los_angeles',"d f j y h s")."<br />"; } while ($current_date_start<$this->date_end); function format_local_date($timestamp,$timezone,$format_str='') { $date_time=new datetime_52("now",new datetimezone($timezone)); $date_time->settimestamp($timestamp); if ($format_str=='') $format_str="f j y"; homecoming $date_time->format($format_str); } // //datetime_52 class // /** * provides backwards back upwards php 5.2's lack of settimestamp , gettimestamp */ class datetime_52 extends datetime{ /** * set time of datetime object unix timestamp * @param int $unixtimestamp * @return datetime_52 */ public function settimestamp($unixtimestamp){ if(!is_numeric($unixtimestamp) && !is_null($unixtimestamp)){ trigger_error('datetime::settimestamp() expects parameter 1 long, '.gettype($unixtimestamp).' given', e_user_warning); } else { $default_timezone=date_default_timezone_get(); $this_timezone= $this->gettimezone(); date_default_timezone_set($this->gettimezone()->getname()); $this->setdate(date('y', $unixtimestamp), date('n', $unixtimestamp), date('d', $unixtimestamp)); $this->settime(date('g', $unixtimestamp), date('i', $unixtimestamp), date('s', $unixtimestamp)); date_default_timezone_set($default_timezone); } homecoming $this; } /** * time of datetime object unix timestamp * @return int unix timestamp representing time in datetime object */ public function gettimestamp(){ homecoming $this->format('u'); } }

and here's output:

sun nov 2 2014 00 00 00 sun nov 2 2014 23 00 00 mon nov 3 2014 23 00 00 tue nov 4 2014 23 00 00 wed nov 5 2014 23 00 00 thu nov 6 2014 23 00 00 fri nov 7 2014 23 00 00 sat nov 8 2014 23 00 00

as can see it's dropped hour. dst border case. thought "+1 day" should handle that. help!

your order of operations wrong. you're adding +1 day utc time, doesn't have dst variance. should convert local timezone first, , add together +1 day, , that'll trick, since calculation within right timezone check automatically expected.

php

No comments:

Post a Comment