top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

Date and time problem in PHP

+2 votes
64 views

I always hate dealing with date/time stuff in php - never get it even close until an hour or two goes by....

anyway I have this:

// get two timestamp values
$exp_time = $_COOKIE[$applid."expire"];
$curr_time = time();
// get the difference
$diff = $exp_time - $curr_time;
// produce a display time of the diff
$time_left = date("h:i:s",$diff);

Currently the results are:

exp_time is 06:55:07
curr_time is 06:12:03
the diff is 2584
All of these are correct.

BUT time_left is 07:43:04 when it should be only "00:43:04". So - where is the hour value of '07' coming from?? And how do I get this right?

posted Oct 6, 2013 by Anderson

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

You should use gmdate() if you want to how many hours left to expire

$time_left = gmdate("H:i:s",$diff);
answer Oct 6, 2013 by Meenal Mishra
Thanks for the quick response, but why do I want to show the time in GMT? However, I did try it, changing the 'time_left' calc to use "gmdate". Now instead of a 7 for hours I have a 12.

exp 07:34:52
curr 06:40:14
diff 3158
left is 12:52:38

The 52:38 is the correct value, but not the 12.
Try this please
 gmdate("H:i:s", $diff%86400)
Doesn't work either.
Similar Questions
+3 votes

I am trying to calculate a date sequence using $i in conjunction with " (so the represented # is "seen" by PHP)

The code results in 2 errors:

Notice: Undefined variable: i_days_ago in test.php on line 9
Notice: Undefined variable: i_days_ago in test.php on line 13

Is there a way to do this without creating these errors and also "NOTICE" errors? The dates being calculated will be used in a database query (used to generate a report based on the activity between the starting and ending dates).

+2 votes

I have a date field on an html form that users may leave blank. If they do leave it blank I want to write the date 01/01/1901 into the mysql table. How can I accomplish this and where in my .php script file should I put the code?

+3 votes

How to print current date and time in C/C++, sample program will help?

+1 vote

This function limits the"execution time" for a script. Is this CPU time or elapsed (wall-clock) time?

I'm asking because I'm running Win7 in a VirtualBox VM under OS X Mavericks. I've got an app that runs a number of scripts, using PHP 5.5.9. All runs correctly, but quite slowly, and a one script reports taking 20 secs or so of elapsed time when I might have expected 2 or 3 at most (as the same script running under OS X does).

I know that there is a note in the online doc for this function that says that under windows, extra time such as database calls is included. Would that include SQLite calls which wouldn't be included for the OS X version?

I've got a windows user whose scripts are actually running out of time, so I may have to add a php_value to the apache config to crank up the time limit.

Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...