Page 1 of 1

Do this code from start time to end time

Posted: Thu Aug 26, 2004 11:52 pm
by Pineriver
Hoping you all can help me out here
I have a start time and an end time in a human readable format of days hours and Am or PM that is from a user generated form that is stored in a database.
What I would like to be able to do is when the time is over the start time but not over the end time, execute the code in the current day.
Now so far I have successfully done this but I am having trouble with the AM and PM times. Here is the code so far...

Code: Select all

$timezone = '-7200'; /// the time difference to the user from the server
$time = time()-$timezone;
///converting to 24 hour time
if($start_a == PM){ $start_hour = ($start_hour+12);  } 

//getting the start time into the unix timestamp format
$start_time = mktime ($start_hour,$start_min,0,date("m",time()- $timezone),date("d",time()- $timezone),date("y",time()- $timezone)); 

//converting to 24 hour time
if($end_a == PM){ $end_hour = ($end_hour +12);  } 

//getting the end time into the unix timestamp format
$end_time = mktime ($end_hour,$end_min,0,date("m",time()- $timezone),date("d",time()- $timezone),date("y",time()- $timezone));

// if the current time is between start time and end time..
if($time > $start_time &&  $time < $end_time){ 

//do this code
echo "do this code";
}else{
//if time time dose not fall within the start and end times do this code
echo "It is not quite time yet to execute  the code";
}
I cant see any errors so far, just thought I would pitch it to the forums before giving up all hope. Thanks

Posted: Fri Aug 27, 2004 6:53 pm
by hawleyjr
if($start_a == PM) Is PM a constant variable or a string? If it is a string try 'PM' if it is a constant please post more code.

Posted: Sat Aug 28, 2004 4:54 pm
by Pineriver
Thank you for replying.
No that didnt work, I also tried saying if($start_a == 'PM' && $end_a == 'AM'){ add a day to the $end_time } since the start time and end time could be from 11:00PM to 5:00AM the next day. Here is the start and end times with the code that do not seem to work...

Code: Select all

<?
 //current time 2:00AM with the timezone differnce
$time='1093672800';

$start_hour='11';
$start_min='00';
$start_a='PM';

$end_hour='5';
$end_min='00';
$end_a='AM';

$timezone = '3600'; /// the time difference to the user from the server 

///converting to 24 hour time 
if($start_a == 'PM'){ $start_hour = ($start_hour+12);  } 

//getting the start time into the unix timestamp format 
$start_time = mktime ($start_hour,$start_min,0,date("m",time()- $timezone),date("d",time()- $timezone),date("y",time()- $timezone)); 

//adding one more day to the end time if it is needed
if($start_a == 'PM' && $end_a == 'AM'){ $plus_p = 896400; }

//converting to 24 hour time 
if($end_a == 'PM'){ $end_hour = ($end_hour +12);  } 

//getting the end time into the unix timestamp format 
$end_time = mktime ($end_hour,$end_min,0,date("m",time()- $timezone),date("d",((time()- $timezone) + $plus_p)),date("y",time()- $timezone)); 

// if the current time is between start time and end time.. 
if($time > $start_time &&  $time < $end_time){ 

//do this code 
echo "do this code"; 
}else{ 
//if time time dose not fall within the start and end times do this code 
echo "It is not quite time yet to execute  the code"; 
} 

?>