PHP Time Calculation

I am trying to figure out how to subtract a set amount of time from a variable before it is entered into the database.  For instance user selects from a time picker 1:00 pm and then the set variable of 1 hour before is calculated leaving the value of StartTime to be entered in the database as 12 am.

starttime is the variable from the time picker
preptime is the variable that states X amount of minutes prior to starttime to enter into database for the actual start time.


$res->start_date= $start_date;
		$res->end_date = $end_date;
		$res->start = $_POST['starttime'];
		$res->end	= $_POST['endtime'];

Open in new window

BIvsAIAsked:
Who is Participating?
 
BIvsAIAuthor Commented:
Thanks everyone.. I ended up solving it on my own.. turns out all I had to do was a subtract of starttime minus preptime variables.  Or in short for hardcoded values add - 60 for the 1 hour in the end of the $res start line.

Thanks for the great input.
0
 
Dave BaldwinFixer of ProblemsCommented:
For time and date arithmetic, I convert date/times into Unix timestamps because that's the only format where the arithmetic is easy.  Since it appears that you are getting text from a web page, 'strtotime' http://us3.php.net/manual/en/function.strtotime.php is what you want.

Also date() http://us3.php.net/manual/en/function.date.php
$strtime = strtotime($start_date);
// $preptime is in seconds 60 second = 1 minutes, 3600 seconds = 1 hour.
$prep = $strtime - $preptime;
// $prep is now the calculated timestamp before $start_date
// convert to another format using the date() function
$prepdate = date("Y-m-d H:m:s",$prep);

Open in new window

0
 
Ray PaseurCommented:
Many of the DATETIME calculations are described in this article.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

You may want to consider using date_default_timezone_set() as documented on the PHP man page here.
http://php.net/date_default_timezone_set
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Sandeep KothariProject LeadCommented:
try this logic... mite work ...

$st = array('0'=>'12:00 am','1'=>'1:00 am',... '23'=>'11:00 pm');

$pre = array('0'=>1,'1'=>2.... '22'=>23);

$k = array_keys($st,$_POST['starttime']);
$d = array_keys($pre,$_POST['pretime']);

$diff = $k[0] - $d[0];

if($diff < 0){
echo 'you cannot start yesterday';
}

$starttime = $st[$diff];
echo $starttime; 

Open in new window

0
 
Ray PaseurCommented:
Sidebar note (I just re-read this question) 12:00am is midnight.  12:00pm is noon.
0
 
Sandeep KothariProject LeadCommented:
a slight change in the logic

$st = array('0'=>'12:00 am','1'=>'1:00 am',... '23'=>'11:00 pm');

$pre = array('0'=>1,'1'=>2.... '22'=>23);

$k = array_keys($st,$_POST['starttime']);
$d = $_POST['pretime'];

$diff = $k[0] - $d;

if($diff < 0){
echo 'you cannot start yesterday';
}

$starttime = $st[$diff];
echo $starttime; 

Open in new window

0
 
BIvsAIAuthor Commented:
Great thoughts on the solution but in the long run the solution was just a modification of the original line for start time and a long function was not needed to make the calculation.  Value picked minus Value reduced by
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.