简体   繁体   中英

PHP now + 60 days to MYSQL DATETIME

I'm struggling to get the current date/time + 60 days in PHP and to store it in MYSQL, its coming up as all 0's in the MYSQL table.

I've had to settle on doing it directly in the MYSQL insert in my PHP code by doing the following :

DATE_ADD(NOW(), INTERVAL 60 DAY)

The problem is that I want to do an if else statement in PHP and add different number of days to the current date time.

Edited :

Here is the PHP code I was using :

$expirydate = date("Ymd"); // current date

$expirydate = strtotime(date("Ymd", strtotime($expirydate)) . " +60 days");

I then inserted $expirydate in to my expiry_date DATETIME field in MYSQL.

The resulting expiry_date field equals 0000-00-00 00:00:00

$date = new DateTime();
$date->add(new DateInterval('P60D'));
$date->format('Y-m-d');

or

$date = new DateTime();
$date->modify('+60 days');
$date->format('Y-m-d');

What have you tried in PHP?

One way is:

$days = 60;
$time = time() + $days * 86400;
echo date('r', $time);

I use something like this:

$date = date ( 'Y-m-d' );
$newdate = strtotime ( '+60 days' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-d' , $newdate );

You should be more specific and show more PHP code. Anyway it's not hard to add a variable number of days in PHP to NOW :

$days = 6;
$now = new DateTime();
$interval = new DateInterval("P$daysD");

// Clone!!!
$dateNext = clone $now;
$dateNext->add($interval);

// Cloning is necessary if you want to use it in a loop
// because add will modify original object (thus $now)
for($i = 1; $i <= 10; $i++)
{
    $modified = clone $now;

    // Add $i days
    $modified->add(new DateInterval("P$iD"));

    // Store in database
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM