Do this code from start time to end 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
Pineriver
Forum Commoner
Posts: 50
Joined: Fri Aug 15, 2003 5:24 pm

Do this code from start time to end time

Post 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
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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.
Pineriver
Forum Commoner
Posts: 50
Joined: Fri Aug 15, 2003 5:24 pm

Post 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"; 
} 

?>
Post Reply