add time to timestamp
Moderator: General Moderators
-
networkguy
- Forum Newbie
- Posts: 19
- Joined: Tue Mar 31, 2009 2:26 am
add time to timestamp
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!
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!
-
networkguy
- Forum Newbie
- Posts: 19
- Joined: Tue Mar 31, 2009 2:26 am
Re: add time to timestamp
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?
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?
-
networkguy
- Forum Newbie
- Posts: 19
- Joined: Tue Mar 31, 2009 2:26 am
Re: add time to timestamp
Actually, forget it I figured it out.
Thanks anyways!
Thanks anyways!
- greyhoundcode
- Forum Regular
- Posts: 613
- Joined: Mon Feb 11, 2008 4:22 am
Re: add time to timestamp
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
strtotime() might simplify things for you.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Re: add time to timestamp
aka time()greyhoundcode wrote:Code: Select all
mktime(date('H'), date('i'), date('s'), date('m'), date('d'), date('Y'))
Re: add time to timestamp
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()greyhoundcode wrote:Code: Select all
mktime(date('H'), date('i'), date('s'), date('m'), date('d'), date('Y'))
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
- greyhoundcode
- Forum Regular
- Posts: 613
- Joined: Mon Feb 11, 2008 4:22 am
Re: add time to timestamp
-
networkguy
- Forum Newbie
- Posts: 19
- Joined: Tue Mar 31, 2009 2:26 am
Re: add time to timestamp
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".