简体   繁体   中英

add one year to datetime with php

$data['user']['time'] = '2011-03-07 00:33:45';    

how can we add 1 year to this date ?

something like $newdata = $data['user']['time'] + 1 year ?

or

$newdata = 2012-03-07 00:33:45

Thanks

Adam Ramadhan

strtotime()是你正在寻找的功能:

$data['user']['seal_data'] = date('Y-m-d H:i:s', strtotime('+1 year', strtotime($data['user']['time'])));

First, you have to convert the MySQL datetime to something that PHP can understand. There are two ways of doing this...

  1. Use UNIX_TIMESTAMP() in your query to tell MySQL to return a UNIX timestamp of the datetime column.

     SELECT whatever, UNIX_TIMESTAMP(myTime) AS 'myUnixTime' FROM myTable; 
  2. Use DateTime::createFromFormat to convert your string time to something PHP can understand.

     $date = DateTime::createFromFormat('Ymd H:i:s', $data['user']['time']); 

Once that is done, you can work with the time... Depending on the method you used above, you can use one of the following.

  1. If you have a unix timestamp, you can use the following to add a year:

     $inAYear = strtotime('+1 year', $data['user']['unixTime']); 
  2. If you have a DateTime object, you can use the following:

     $inAYear = $date->add(new DateInterval('P1Y')); 

Now, to display your date in a format that is respectable, you must tell PHP to return a string in the proper format.

  1. If you have a unix timestamp, you can use the following:

     $strTime = date('Ymd H:i:s', $inAYear); 
  2. If you have a DateTime object, you can use the following:

     $strTime = $inAYear->format('Ymd H:i:s'); 

Alternatively, if you don't want to deal with all of that, you can simply add one year when you query.

SELECT whatever, DATE_ADD(myTime, INTERVAL 1 YEAR) AS 'inAYear' FROM myTable;

Current (2017) Practice is to use DateTime

This question is top on a google search for "php datetime add one year", but severely outdated . While most of the previous answers will work fine for most cases, the established standard is to use DateTime objects for this instead, primarily due strtotime requiring careful manipulation of timezones and DST.

TL;DR

  1. Convert to DateTime : $date = new DateTime('2011-03-07 00:33:45', [user TZ]);
  2. Use DateTime::modify : $date->modify('+1 year');
  3. Format to needs.

Following this pattern for manipulating dates and times will handle the worst oddities of timezone/DST/leap-time for you.

Just remember two final notes:

  • Life is easier with your system timezone set at UTC .
  • NEVER modify the system timezone outside of configuration files.
    • I've seen too much code that relies on date_default_timezone_set . If you're doing this, stop. Save the timezone in a variable, and pass it around your application instead, please .

More Reading

How to calculate the difference between two dates using PHP?

Convert date format yyyy-mm-dd => dd-mm-yyyy

PHP - strtotime, specify timezone

I think you could use strtotime() to do this pretty easily. Something like:

$newdata = date('c', strtotime($data['user']['time'] . ' +1 year'));

Though the 'c' format string isn't the same as your input format. You could consult date() 's docs for how to construct the correct one.

'Ymd H:i:s' — as Tim Cooper suggests — looks correct.

This should do the trick (not tested).

$data = "2011-03-07 00:33:45";

echo 'Original date +1 year: ' . date('Y-m-d H:i:s', strtotime(date("Y-m-d H:i:s", strtotime($data)) . " +1 year"));

First-of-all if your date format is separated by a slash (/), like '2019/12/31' then you should convert it in dash (-) format, like ' 2019-12-31 ', to do so use str_replace() function.

$string = str_replace('/', '-', '2019/12/31'); //output: 2019-12-31

To add time/day/month/year do not use strtotime() function, because it can't add a time which is beyond year 2038 . So here I would prefer to use DateTime() function.

$string = '2000-01-01';
$date = new DateTime($string);
$date->add(new DateInterval('P60Y5M2DT6H3M25S')); //60 Years 5 Months 2 Days 6 Hours 3 Minutes 25 Seconds
echo $date->format('Y-m-d H:i:s'); //output: 2060-06-03 06:03:25

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