add time to timestamp

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
networkguy
Forum Newbie
Posts: 19
Joined: Tue Mar 31, 2009 2:26 am

add time to timestamp

Post 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!
networkguy
Forum Newbie
Posts: 19
Joined: Tue Mar 31, 2009 2:26 am

Re: add time to timestamp

Post 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?
networkguy
Forum Newbie
Posts: 19
Joined: Tue Mar 31, 2009 2:26 am

Re: add time to timestamp

Post by networkguy »

Actually, forget it I figured it out.


Thanks anyways! :banghead:
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: add time to timestamp

Post 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;
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: add time to timestamp

Post by pickle »

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.
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: add time to timestamp

Post by Apollo »

greyhoundcode wrote:

Code: Select all

mktime(date('H'),
    date('i'),
    date('s'),
    date('m'),
    date('d'),
    date('Y'))
aka time() :)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: add time to timestamp

Post 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()
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: add time to timestamp

Post by greyhoundcode »

:o Fair one
networkguy
Forum Newbie
Posts: 19
Joined: Tue Mar 31, 2009 2:26 am

Re: add time to timestamp

Post 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".
Post Reply