Solved

PHP DATE time add a year

Posted on 2015-02-21
2
132 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 110

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 110

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with mod_substitute 18 77
if statement malfunction 5 44
Echo'd value as selected in dropdown 20 44
Windows 10, 64 bit WAMP - won't start 9 30
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This article discusses how to implement server side field validation and display customized error messages to the client.
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

751 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