Jump to content

Date functions...adding 24hs to an already generated date and such, need help.


nightkarnation

Recommended Posts

Hello Guys!

Sorry about the Subject, didn't know how to explain it in few words...

So here's the deal...

 

$last_free_date = "Mon Jun 25 20:12:25 GMT-0300 2012"
$server_current_time = date('D M j H:i:s \G\M\TO Y'); //will have the same format as the $last_free_date variable
//Now here is what I need to do:
//$last_format_date = $last_free_date + 24hs

 

Any ideas?? I need to add $last_free_date + 24hs to the $last_format_date variable...

Thanks a lot in advance!

Cheers!

Link to comment
Share on other sites

There's a couple of ways to do it---100 ways to skin a cat, right?

 

You can always use the UNIXtime and just do the math to add:

1 minute                                 = 60 seconds

1 hour     = 60min*60sec = 3600 seconds

1 day = 24*60min*60 sec = 86400 seconds

1 Week      =7*24*60*60 = 604800 seconds

But the caveat to that is unless you do a little work to it first, the UNIXdate is a TIMESTAMP, so it returns the current time, so the math will be from the current time, not 00:00 of the date.

 

The other option, and one that is becoming more popular, is to use the PHP date/time object (which also has procedural style capabilites).  This is newer, so if you're running on 4.x you'll have to upgrade to 5.x.

 

Object oriented style:

<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P10D'));
echo $date->format('Y-m-d') . "\n";
?> 

Procedural style:

<?php
$date = date_create('2000-01-01');
date_add($date, date_interval_create_from_date_string('10 days'));
echo date_format($date, 'Y-m-d');
?> 

Both result in the same output

 

PHP manual page:

http://www.php.net/manual/en/datetime.add.php

Link to comment
Share on other sites

assuming you have your date already formated 'Y-m-d' in a variable (here it will be $date)

 

$newDate = date_add($date, date_interval_create_from_date_string('1 day'));

 

From there you have the new date to reformat, output, do what you please

 

There are lots of examples on ways to use date_add at the php manual page for it:

http://www.php.net/manual/en/function.date-add.php

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.