bcamp1973 Posted April 8, 2007 Share Posted April 8, 2007 I'm trying to create a function that returns the local date/time if you feed it a GMT date/time and the local offset. For example i live in MST so i think it's GMT-7. This is the function i've created, but it's returning weird values. didn't work for me last night, but mysteriously worked this morning. maybe something screwy with AM vs PM? I'm passing the date in the format "Y-m-d H:i:s". I'm not so good with date calculations obviously... function mydate($date,$offset,$dst){ $date=explode(' ',$date); list($year,$month,$day)=explode('-',$date[0]); list($hour,$minute,$second)=explode(':',$date[1]); if(($hour+$offset)>0){ $hour=$hour+$offset; } else { $hour=($hour+$offset)*-1; $day--; } if($dst) $hour=$hour+date('I'); $yourdate=strtotime($year.'-'.$month.'-'.str_pad($day,2,'0',STR_PAD_LEFT).' '.str_pad($hour,2,'0',STR_PAD_LEFT).':'.$minute.':'.$second); if($day==gmdate('j')){ return 'Today @ '.date('g:i A',$yourdate); } elseif($day==(gmdate('j')-1)){ return 'Yesterday @ '.date('g:i A',$yourdate); } else { return date('M jS, Y @ g:i A',$yourdate); } } Link to comment https://forums.phpfreaks.com/topic/46141-determining-local-datetime-given-gmt-and-offset/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.