Which Time Function To Use

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
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Which Time Function To Use

Post by JakeJ »

There seem to be several time functions out there and I'm not sure which will suit my needs best.

I need to add $months to a time stamp for now().

I need to output a future date $months from now.

Thanks!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Which Time Function To Use

Post by requinix »

strtotime() converts an English string to a Unix timestamp.

Code: Select all

$months = 5;
$time = strtotime("+{$months} months");
echo date("Y-m-d H:i:s", $time);
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Which Time Function To Use

Post by onion2k »

JakeJ wrote:I need to add $months to a time stamp for now().
NOW() is a MySQL function. If you're doing this in MySQL, use DATE_ADD.
pbs
Forum Contributor
Posts: 230
Joined: Fri Nov 07, 2008 5:31 am
Location: Nashik, India
Contact:

Re: Which Time Function To Use

Post by pbs »

I thing you need my sql function, so use:

Code: Select all

 
DATE_ADD( NOW( ) , INTERVAL 1 MONTH );
 
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Which Time Function To Use

Post by JakeJ »

tasairis wrote:strtotime() converts an English string to a Unix timestamp.

Code: Select all

$months = 5;
$time = strtotime("+{$months} months");
echo date("Y-m-d H:i:s", $time);
That did it for me! Muchas Gracias!
Post Reply