PHP date

I am having a little trouble converting UNIX time into a different format.
My query returns a date in this format: 1427836488
That is UNIX Date / Time correct?

To format it 2015-04-01 do I write it like this?
$sd = date('Y-m-d', $row['entry_date']);

Open in new window

LVL 8
rgranlundAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Radek BaranowskiFull-stack Java DeveloperCommented:
yes, it should work.
skijCommented:
This:
<?php

echo date('Y-m-d', 1427836488)

?>

Open in new window

returns 2015-03-31 not 2015-03-31.  Make sure that you are making the proper offset for time-zones.   The Unix time stamp is reordered for the UTC time zone.
rgranlundAuthor Commented:
@radek
The following does not work though.  How do I finish that up?
 $new_business_xml.= "<ExpirationDt>".date('Y-m-d', '+ 1 year')."</ExpirationDt>";

Open in new window

I need to add one year to the date.
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Radek BaranowskiFull-stack Java DeveloperCommented:
try

"<ExpirationDt>".(date('Y','1427836488')+1)."-".date('m-d','1427836488')."</ExpirationDt>";
Chris StanyonWebDevCommented:
Procedural 'old-school' way of doing it:

$timestamp = 1427836488;
echo date('Y-m-d', strtotime('+1 year', $timestamp));

Open in new window


New, (PHP >5.3.0) OOP way of doing it:

$timestamp = 1427836488;
$date = new DateTime("@".$timestamp);
echo $date->add(new DateInterval('P1Y'))->format("Y-m-d");

Open in new window

skijCommented:
There are 31536000 in standard (non-leap) years.

You could add that to your database result like this:
$new_business_xml.= "<ExpirationDt>".  date('Y-m-d', $row['entry_date'] + 31536000)  ."</ExpirationDt>";

Open in new window

rgranlundAuthor Commented:
Can I do to get todays date:
$timestamp = date('Y-m-d');
echo date('Y-m-d', strtotime('+1 year', $timestamp));
Chris StanyonWebDevCommented:
To add a year to todays date:

date('Y-m-d', strtotime('+1 year'))

or

$date = new DateTime();
echo $date->add(new DateInterval('P1Y'))->format('Y-m-d');
Ray PaseurCommented:
This is not an uncommon question, so we have an article with all sorts of examples.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
Ray PaseurCommented:
If you read the article and still have questions about how to do date / time arithmetic in PHP, please post back and we will be glad to help.  It's got lots of code examples related to date / time arithmetic, including ways of handling the inherent ambiguities of words like "month" or "year" - these have different numbers of days, and it's worth understanding what PHP will do with those terms when used in strtotime() or the DateTime class and its ilk.

Please see: http://iconoun.com/demo/temp_rgranlund.php
<?php // demo/temp_rgranlund.php

/**
 * See: http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28648127.html
 */
error_reporting(E_ALL);

// FROM THE QUESTION AT E-E
$timestamp = 1427836488;

// COPY IT INTO ANOTHER VARIABLE
$row['entry_date'] = $timestamp;

// FROM THE QUESTION AT E-E
$sd = date('Y-m-d', $row['entry_date']);
echo "<br>sd: $sd";

// A MORE FINE-GRAINED VIEW
$datec = date('c', $row['entry_date']);
echo "<br>datec: $datec";

// ADDING A YEAR
$year = date('c', strtotime($datec . ' + 1 year'));
echo "<br>year: $year";

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.