user1052096
user1052096

Reputation: 883

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("Y-m-d"); // current date

$expirydate = strtotime(date("Y-m-d", 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

Upvotes: 3

Views: 7192

Answers (4)

Giannis
Giannis

Reputation: 813

I use something like this:

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

Upvotes: 2

gremo
gremo

Reputation: 48899

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
}

Upvotes: 1

Umbrella
Umbrella

Reputation: 4788

What have you tried in PHP?

One way is:

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

Upvotes: 2

PeeHaa
PeeHaa

Reputation: 72652

$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');

Upvotes: 3

Related Questions