Solved

PHP DATE time add a year

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sql query on a varchar that is numeric. 8 28
How do I put a session for a radio button? 4 15
datetime in sql 6 31
Method for submitting dynamically created rows 20 18
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

863 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

28 Experts available now in Live!

Get 1:1 Help Now