简体   繁体   中英

add 30 days to a variable in sql then echo in php

Like an expiration date for a subscribed member.

May sound simple but all i get is the 31st january 1969!

eg.

<? $expire = strtotime("+30 days",$subscribed); echo $expire;?>

The SQL entry is already using the YMD format and works fine when just echoed as a variable.

But when i try to add 30 days to the $subscribed value all i get is a bunch of numbers which when put through the .date function comes up with 31-01-1969

Probably an easy solution but i'm a noob lol.

Appreciate any help :)

you have to tell PHP that your string is a date and use the datetime object :

<?php
$date = new DateTime($subscribed);
$date->add(new DateInterval('P30D'));
$expire = $date->format('Y-m-d');
echo $expire;
?>

try this:

$expire = date('Y-m-d', strtotime($subscribed. ' + 30 days'));
echo $expire;

read more about strtotime

The second parameter of strtotime should be the timestamp used as a base for the calculation of relative dates

Try this.

$expire = date('Y-m-d', strtotime("+30 days", strtotime($subscribed)));
echo $expire;

Use below code to add 30 days with your date

<?php
$expire = date('Y-m-d', strtotime($subscribed . " +30 days"));
echo $expire;
?>

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