Jump to content

addition to date?


$php_mysql$

Recommended Posts

how do i add 30 days from current date?

 

$newdate = date("D, j F,  Y", $date); displays

 

Thu, 18 August, 2011

 

now with a new variable how do i display a date 30 days from Thu, 18 August, 2011 which would be September something?

 

i tried

 

$newdate = date("D, j F,  Y", $date);

 

$newdate2 = date("D, j F,  Y", time() + $date);

 

but it display Mon, 26 February, 1917

Link to comment
Share on other sites

try

<?php
echo 'Now:       '. date('Y-m-d') ."\n";
echo 'in 30 days: '. date('Y-m-d', strtotime('+30 days')) ."<br>";
// Or
echo 'in 30 days: '. date('Y-m-d', time() + 2592000) ."<br>";
?>

 

my output was:

 

Now: 2011-08-18

in 30 days: 2011-09-17

in 30 days: 2011-09-17

 

More Information on the functions:

TIME() - http://php.net/manual/en/function.time.php

strtotime() - http://php.net/manual/en/function.strtotime.php

 

Link to comment
Share on other sites

<?

$newdate = date("D, j F,  Y", $date); //compare this 

 

$addtiondate = date("D, j F,  Y", strtotime('+30 days')); // to this

 

if(strtotime($addtiondate) > strtotime($newdate)){

    // bla bla

}

?>

 

thanks but result show just 1 where as 30 days from today would be Sat, 17 September, 2011

 

i did this

 

$newdate = date("D, j F,  Y", $date); //compare this   
$addtiondate = date("D, j F,  Y", strtotime('+30 days')); // to this
$addition = strtotime($addtiondate) > strtotime($newdate); 

echo $addition ;

Link to comment
Share on other sites

Why are you doing multiple conversions on both values to compare dates? If your dates are stored in your database as either a mysql DATE, DATETIME, or TIMESTAMP data type, you can do greater-than/less-than comparisons directly on the values, as someone explained in one of your previous threads, provided both values being compared are in the same format.

 

What exactly are you trying to accomplish by comparing dates?

Link to comment
Share on other sites

Not sure what you trying to achieve with this line

$addition = strtotime($addtiondate) > strtotime($newdate); 

 

but $addition variable will have a boolean value based on the condition. In this case it will be "true" since the $additiondate is greater than $newdate.

Link to comment
Share on other sites

no this time is not stored in mysql DATETIME format,  stored the time using time() so the some without conversion shows something like 1234567 etc,

 

$newdate = date("D, j F,  Y", $date); //this line converts the time to Thu, 18 August, 2011 format

 

so what im trying is getting 30 days from $newdate  which  prints the result as Thu, 18 August, 2011 and i want Sat, 17 September, 2011

 

 

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.