gmdate and time

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
SmokyBarnable
Forum Contributor
Posts: 105
Joined: Wed Nov 01, 2006 5:44 pm

gmdate and time

Post by SmokyBarnable »

What is this code doing?

Code: Select all

$req->StartTimeFrom = gmdate('Y-m-d H:i:s', time() - 60 * 60 * 24 * 15);
$req->StartTimeTo   = gmdate('Y-m-d H:i:s');
It looks like its creating a date range....how could I adjust the date range?

Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

StartTimeFrom is a date and time in the past. StartTimeTo is the current date and time. Both are shifted to Greenwich time.
User avatar
SmokyBarnable
Forum Contributor
Posts: 105
Joined: Wed Nov 01, 2006 5:44 pm

Post by SmokyBarnable »

I live in the central time zone in the United States. I think it is +6 difference relative to GMT. How would I convert the current GMT to my time zone?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Use date() or subtract six hours from the time given to the function.
User avatar
SmokyBarnable
Forum Contributor
Posts: 105
Joined: Wed Nov 01, 2006 5:44 pm

Post by SmokyBarnable »

Like this?

Code: Select all

function toGMT($time){
	//set to local time zone relative to GMT (+6 for US CST)
	$step = 6;	
	$mytime = substr($time,11);
	$time_array = explode(":",$mytime);		
	$mydate = substr($time,0,10);
	$date_array = explode("-",$mydate);
	$newtime = date("Y-m-d H:i:s",mktime($time_array[0]-$step,$time_array[1],$time_array[2],$date_array[1],$date_array[2],$date_array[0]));
	return $newtime;
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Tested it?
User avatar
SmokyBarnable
Forum Contributor
Posts: 105
Joined: Wed Nov 01, 2006 5:44 pm

Post by SmokyBarnable »

Yes it works...just wanted to see if it was the best way.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It's a way. I can't say whether it's the best way, nor does it really matter. ;)
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

8O

Code: Select all

$time-=6*60*60; // if $time is a timestamp

$time = date("Y-m-d H:i:s",strtotime($time.' -6 hours')); // if time begins in the other format
Post Reply