Solved

PHP DATE time add a year

Posted on 2015-02-21
2
118 Views
Last Modified: 2015-02-21
I have a query that returns an entry date that is in UNIX time  I want to convert the time to human time and the following works:
$start_date = $row['entry_date'];
$sd = gmdate(' l F d Y', $start_date);

I want to create a new variable that adds one year to the returned date.
0
Comment
Question by:rgranlund
  • 2
2 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40623696
Please read this article while I put together an answer for you:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

If you want to post the data -- the contents of $row['entry_date'] -- I'll have a much better chance of helping you!
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40623701
Let's assume that the contents of $row['entry_date'] is a UNIX timestamp**

Something close to this untested code will convert it into the ISO-8601 datetime value, then add a year.

$sd = date('c', strtotime( date('c', $row['entry_date']) . ' + 1 year'));

The inner part of the expression is this:

date('c', $row['entry_date']) . ' + 1 year'));

It says, "take the UNIX timestamp value that is in $row['entry_date'] and convert it into the ISO-8601 string designated by the 'c' format of the date() function.  Then concatenate the ' + 1 year' string to the ISO-8601 value.  Today this would look something like '2015-02-21 18:04:00 + 1 year'

Next the outer part of the expression uses strtotime() to process that string, creating a UNIX timestamp for the future value (+1 year).

And finally the date('c', ...) function will reformat the new (+1 year) timestamp value into an new ISO-8601 string.  You can change the value from 'c' to any formatting value that is supported by the date() function if you want something other than the ISO standard.  The formatting codes are given here:
http://php.net/manual/en/function.date.php

** A good way to store datetime values in a database is to use the ISO-8601 format instead of a UNIX timestamp.  The ISO standard is easier to read.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question