Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add one year to datetime with php

Tags:

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

like image 461
Adam Ramadhan Avatar asked Mar 06 '11 17:03

Adam Ramadhan


People also ask

How to add 1 year to a date in php?

we will help you to give example of php add 1 year to current date. $date = "2022-02-01"; $newDate = date('Y-m-d', strtotime($date.

How to get year of date in php?

If you don't want to use the DateTime class then you can simply use the PHP's date() function to get the year from date.

What is the format of DateTime?

For example, the "d" standard format string indicates that a date and time value is to be displayed using a short date pattern. For the invariant culture, this pattern is "MM/dd/yyyy". For the fr-FR culture, it is "dd/MM/yyyy". For the ja-JP culture, it is "yyyy/MM/dd".

How can I compare two dates in if condition in PHP?

Comparing two dates in PHP is simple when both the dates are in the same format but the problem arises when both dates are in a different format. Method 1: If the given dates are in the same format then use a simple comparison operator to compare the dates. echo "$date1 is older than $date2" ; ?>


2 Answers

strtotime() is the function you're looking for:

$data['user']['seal_data'] = date('Y-m-d H:i:s', strtotime('+1 year', strtotime($data['user']['time'])));
like image 51
Tim Cooper Avatar answered Oct 28 '22 06:10

Tim Cooper


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('Y-m-d 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('Y-m-d H:i:s', $inAYear);
    
  2. If you have a DateTime object, you can use the following:

    $strTime = $inAYear->format('Y-m-d 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;
like image 12
Andrew Moore Avatar answered Oct 28 '22 08:10

Andrew Moore