Page 1 of 1
Converting and comparing times [Solved]
Posted: Fri Dec 30, 2005 6:58 pm
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
Posted: Fri Dec 30, 2005 8:16 pm
by Chris Corbyn
Hopefully this will help...
strtotime()

Posted: Sat Dec 31, 2005 7:46 am
by dwessell
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
Posted: Sat Dec 31, 2005 9:43 am
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
Posted: Sat Dec 31, 2005 10:33 am
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";
}
}