[RESOLVED] How do you add 14 days to a $today script?

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
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

[RESOLVED] How do you add 14 days to a $today script?

Post by simonmlewis »

Hi

PHP's well used:

Code: Select all

$today = (date('Y-m-d'));
Is useful to find the date in the system.

However, what do I do to gather the date in a fortnight's time??

Simon
Last edited by simonmlewis on Wed Jul 22, 2009 3:12 pm, edited 1 time in total.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
mischievous
Forum Commoner
Posts: 71
Joined: Sun Apr 19, 2009 8:59 pm

Re: How do you add 14 days to a $today script?

Post by mischievous »

Option #1:

Code: Select all

$today = time();//Unix Time Stamp
$amount_of_days = 14; //Number of days to add
$futuretime = 60 * 60 * 24 * $amount_of_days; //60 seconds times 60 minutes times 24 hours times the number of days ahead
$futuredate = date('Y-m-d', $futuretime) //put in date format
Option #2:

Code: Select all

 
$futuredate = strtotime("+14 day");
 
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you add 14 days to a $today script?

Post by simonmlewis »

Thx.

The first option gives the result:
1970-01-15
The second gives:
1249496533
Something not quite right?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you add 14 days to a $today script?

Post by simonmlewis »

Unless anyone has a better method, think I just found the way - after about an hour of searching:

Code: Select all

<?php
 $fortnight = date("Y-m-d", (time() + (86400*14)));  
 echo $fortnight;
?>
Some mktime methods just produce a load of numbers for some reason. And then you have to stick a function above them.

This method is short and easy.

Simon
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you add 14 days to a $today script?

Post by simonmlewis »

Damn it - just realised I don't a fortnight ahead of today, but a fortnight ahead of a given day.

It's the Pseucode:

Code: Select all

variable = subscription date + 14 days.
    if the result of (variable) <= todayDate, then do something.
Can anyone help please, bearing in mind I have the code below?

Code: Select all

$fortnight = date("Y-m-d", (time() + (86400*14)));
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
spider.nick
Forum Commoner
Posts: 72
Joined: Wed Jul 15, 2009 12:22 pm
Location: Overland Park, KS

Re: How do you add 14 days to a $today script?

Post by spider.nick »

simonmlewis wrote:Damn it - just realised I don't a fortnight ahead of today, but a fortnight ahead of a given day.

It's the Pseucode:

Code: Select all

variable = subscription date + 14 days.
    if the result of (variable) <= todayDate, then do something.
Can anyone help please, bearing in mind I have the code below?

Code: Select all

$fortnight = date("Y-m-d", (time() + (86400*14)));
The following should work:

Code: Select all

 
$subDateMicrotime = strtotime('subscription date');
$fortnight = date( "Y-m-d", ($subDateMicrotime + (86400*14)) );
 
Post back to let me know how it works.

Nick
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you add 14 days to a $today script?

Post by simonmlewis »

That works - however I now now have stage two to complete.

I need to do an if statement that asks if the $subscriptiondate <= $fortnight.

They get the system for free for a fortnight, so I need to see if they have used it for a fortnight. If they haven't yet, just pass them through, if they have gone over that, then send them somewhere else.

I can do the latter part easy - it's just the IF I need to sort.
I have tried this:

Code: Select all

    $timeStamp = strtotime("$row->subscribed");
  $timeStamp += 24 * 60 * 60 * 14;
  $fortnight = date("Y-m-d", $timeStamp);
if ($timestamp <= $fortnight) {......................
 
But for the date I have for this person, and the date on my system, it should reject the person, but it isn't. The date on the system is mid Sept, and the date for the person is 2009-09-02. Basically, it's more than a fortnight.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
spider.nick
Forum Commoner
Posts: 72
Joined: Wed Jul 15, 2009 12:22 pm
Location: Overland Park, KS

Re: How do you add 14 days to a $today script?

Post by spider.nick »

simonmlewis wrote:That works - however I now now have stage two to complete.
Good!

Now, go back to your first post, and click the 'Edit' button. Change the post title from How do you add 14 days to a $today script? to [RESOLVED] How do you add 14 days to a $today script?.

Once you do that, make sure to start a new post with your new question.

Nick
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: [RESOLVED] How do you add 14 days to a $today script?

Post by Weirdan »

If you need fortnight you may ask for just that:

Code: Select all

 
var_dump(date("Y-m-d", strtotime("next fortnight", strtotime("2008-02-10"))));
 
Post Reply