Jump to content

determining local date/time, given GMT and offset


bcamp1973

Recommended Posts

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

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.