Page 1 of 1
add time to timestamp
Posted: Thu Apr 09, 2009 10:58 am
by networkguy
I was wondering if anyone could help me with this:
I have a timestamp in this format: date( "F, j, Y g:i:a"); for a given record.
I would like to add 12 hours to that so that I could keep track of if a subroutine should execute or not.
Each time the conditional statement approaches it should check the return value, if greater then 12 hours then don't execute, else execute.
A time difference function is needed?
Check current time against timestamp?
I've never done much of anything with the date function in any language, never mind php. Now I would like to learn. Any help, pointers or suggestions would be appreciated.
Thanks in advance for any reply!
Re: add time to timestamp
Posted: Thu Apr 09, 2009 12:08 pm
by networkguy
After searching a bit, I find mktime() function will do what I need.
How would I mktime() with the current time?
mktime(time()) ? The arguments for mktime() seem to only take int values. Do I pass in int values from the date() + time() function?
Re: add time to timestamp
Posted: Thu Apr 09, 2009 12:37 pm
by networkguy
Actually, forget it I figured it out.
Thanks anyways!

Re: add time to timestamp
Posted: Thu Apr 09, 2009 12:53 pm
by greyhoundcode
I see you've figured it out - gonna post this anyway for anyone else coming across the thread, just a corruption of something in the
good book itself.
Code: Select all
$date = date("F, j, Y g:i:a",
mktime(date('H'),
date('i'),
date('s'),
date('m'),
date('d'),
date('Y'))
+ (60 * 60 * 12));
echo $date;
Re: add time to timestamp
Posted: Thu Apr 09, 2009 2:07 pm
by pickle
strtotime() might simplify things for you.
Re: add time to timestamp
Posted: Thu Apr 09, 2009 2:19 pm
by Apollo
greyhoundcode wrote:Code: Select all
mktime(date('H'),
date('i'),
date('s'),
date('m'),
date('d'),
date('Y'))
aka
time() 
Re: add time to timestamp
Posted: Thu Apr 09, 2009 2:59 pm
by pickle
greyhoundcode wrote:Code: Select all
mktime(date('H'),
date('i'),
date('s'),
date('m'),
date('d'),
date('Y'))
You've got to be careful about that, as each call to date() can use a different timestamp. So it's possible the hour, minute, second, year, etc aren't for the same instant in time. It's best to call time() first, store that in a variable, then use that variable as the time source for date()
Re: add time to timestamp
Posted: Thu Apr 09, 2009 4:44 pm
by greyhoundcode

Fair one
Re: add time to timestamp
Posted: Sat Apr 11, 2009 1:02 am
by networkguy
The answer I figured out is simple. I'd increment the day or hours using date() to set the "expiry", convert it to int (using maketime() by first using impode on the output of date, storing it in a array, then passing the array elements to maketime()) value and then compare the current date() int value to the the expiry. If the "expiry" is less then "current" then its not "expired", else it is "expired".