Solved

PHP DATE time add a year

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

Expert Comment

by:Ray Paseur
Comment Utility
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 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now