Converting and comparing times [Solved]

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
dwessell
Forum Commoner
Posts: 62
Joined: Fri Dec 23, 2005 2:30 pm

Converting and comparing times [Solved]

Post by dwessell »

And it's me again..

I have a situation where I'm asking a user for a date and a time to schedule an event.. So far I have it stored in the database like this:
Name:Type:Format

Date (Date) yyyy-mm-dd
Time (Time) 01:00:00
Time 2 (text) am or pm
TimeZone (text) Eastern, Mountain etc..

When I'm checking to see if we need to start the event, I"m getting hung up on the time.. And getting apples to apples.. Can someone make a suggestion?

Thanks
David
Last edited by dwessell on Sat Dec 31, 2005 10:33 am, edited 1 time in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Hopefully this will help... strtotime()

;)
dwessell
Forum Commoner
Posts: 62
Joined: Fri Dec 23, 2005 2:30 pm

Post by dwessell »

d11wtq wrote:Hopefully this will help... strtotime()

;)
Hey d11wtq.. Thanks as always..

Here's what I've got so far, and I'm getting close, but not quite there.

Code: Select all

$timeConvert = "$row[6] $row[7], $row[2]";
	echo "$timeConvert";
	$timeStamp = strtotime($timeConvert);
	echo "$timeStamp";
This ends up with a value in $timeConvert of "09:00:00 am, 2005-12-31-1"

Row 6 is 09:00:00
Row 7 is am
Row 2 is 2005-12-31.

strtotime is returning a -1, so I know I'm not feeding it the correct information.. I'll keep playing with it..

Thanks
David
dwessell
Forum Commoner
Posts: 62
Joined: Fri Dec 23, 2005 2:30 pm

Post by dwessell »

Ah HA!! :-)

The outer loop of the code has me checking to make sure that anything that we work with, is today... So I can assume that anything that I pass to strtotime() has a date of today..

Passing in just the time, and not the date works well.. Thanks a ton for the suggestion.. I may have a follow up in a bit, so I'm not going to mark this as solved yet, but will come back and do it shortly.

Thanks
David
dwessell
Forum Commoner
Posts: 62
Joined: Fri Dec 23, 2005 2:30 pm

Post by dwessell »

For future searchers.. Please note, there's an error still in the logic of timezones... But the time conversion works well.

Code: Select all

putenv ('TZ=GMT0'); 
$currentDate = date('Y-m-d');
$currentTime = time();
$query  = "SELECT * FROM `Game Information` WHERE `DATE` = '$currentDate' AND `Status` = 0"; 
$result = mysql_query($query);
while( $row = mysql_fetch_array($result, MYSQL_NUM) ){
	//Put the time in a stampable format
	$timeConvert = "$row[6] $row[7]";
	//Turn it into a Unix Timestamp, should return GMT 0
	$timeStamp = strtotime($timeConvert);
	
	//Compensate for timezone differentials
	if($row[8]="Eastern"){
		$timeStamp -= 18000;
	}
	else if($row[8]="Central"){
		$timeStamp -= 21600;
	}
	else if($row[8]= "Mountain"){
		$timeStamp -= 25200;
	}
	else if($row[8] = "Pacific"){
		$timeStamp -= 28800;
	}
	
	//Now check to see if we need to run a game
	//We check to see if a game is scheduled before now
	//through 15 minutes in the future
	if( ($timeStamp<$currentTime) ||($timeStamp<=($currentTime+900)) ){
		echo "We have a game";
		echo "$timeStamp  and $currentTime";
		
	}
	
}
Post Reply