Solved

PHP DATE time add a year

Posted on 2015-02-21
2
140 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

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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 …

630 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