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
https://forums.phpfreaks.com/topic/245094-addition-to-date/
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
https://forums.phpfreaks.com/topic/245094-addition-to-date/#findComment-1258922
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
https://forums.phpfreaks.com/topic/245094-addition-to-date/#findComment-1258941
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
https://forums.phpfreaks.com/topic/245094-addition-to-date/#findComment-1259052
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
https://forums.phpfreaks.com/topic/245094-addition-to-date/#findComment-1259055
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
https://forums.phpfreaks.com/topic/245094-addition-to-date/#findComment-1259066
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.