Timothy Coetzee Timothy Coetzee - 3 months ago 9
PHP Question

PHP Bookings System - Calculate Time Giving Wrong Result

Im working on a uni project for a simple car rental system. My problem comes in when calculating time (the amount of days and hours for the car rental)

Right now the time (where I am) is 12:15 PM

CODE:

//Test Variables
$ciDate = '2016-08-28';
$ciTime = '13:10';

//calc Time
$calcDate = $ciDate.$ciTime;
$calcDate = strtotime($calcDate);
$remaining = $calcDate - time();
$days_remaining = floor($remaining / 86400);
$hours_remaining = floor(($remaining % 86400) / 3600);
$totPrice = $days_remaining * 4;
//Disp Result
echo $days_remaining. ' Days and ' . $hours_remaining. ' hours';


MY PROBLEM

When the above code gets executed, I get back 2 Days and 5 hours, which is incorrect considering my time now is 12:15 PM 2016/08/26

I'm going to go out on a limb here and guess the problem is due to server time being different than my current time?

Any suggestions on how to fix the above problem, much appreciated.

EDIT:

At the time of writing the correct result should be 2 days and 1 hour

Answer

I don't know what timezone you are in but it appears your server is in a different zone.
You have MANY ways to fix this but here is the code way.

date_default_timezone_set ( string $timezone_identifier );

So if you are in Bagdhad you put

date_default_timezone_set("Asia/Baghdad");

Or whereever you are.

See this link http://php.net/manual/en/timezones.asia.php

Comments