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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Acronis Data Cloud 7.8 Enhances Cyber Protection

A closer look at five essential enhancements that benefit end-users and help MSPs take their cloud data protection business further.

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:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.