Avatar of umaxim
umaxim
Flag for United States of America asked on

php date

Hi i need to convert this date 2009-01-23T03:00:02-05:00 to regular standard date

Second function i searching to convert regular date time to 2009-01-23T03:00:02-05:00
PHP

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
sivagnanam chandrakanth

Here is the sample

<?php
//For first question
	$date=strtotime("2009-01-23T03:00:02-05:00");
	echo date('d-m-Y',$date);
	
//for second question
	
	$lastmod = '2012-11-28 10:53:17'; //MySQL datetime format

$datetime = new DateTime($lastmod);
$result = $datetime->format('Y-m-d\TH:i:sP');

echo $result; //2012-11-28T10:53:17+01:00
?>

Open in new window

umaxim

ASKER
it is working but it always increase time on one hour
2013-10-06T20:38:00-05:00

2013-10-06 21:38:00
Ray Paseur

"Regular standard date" is not a term of art in computer science.  All dates are spelled out in formats that are infinitely malleable, using PHP strtotime() and date() functions.  The background information you need, as well as specific examples are available in this article.  Please read it over and if you still have questions, post back and I'll be glad to help.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
Your help has saved me hundreds of hours of internet surfing.
fblack61
Mahesh Bhutkar

It may be your LOCALE setting issue.

Checkout with your server locale & machine locale.
sivagnanam chandrakanth

the example given me works in my system.. may be 1+ hour issue is due to your server time / timezone settings
Ray Paseur

The PHP server and the SQL server have independent timezone settings.  Your client location may also have an independent timezone (Client in New York, Server in Chicago).  There is a difference between the value of PHP time() (UNIX  Timestamp) and the local timezone.  The UNIX time is the same worldwide, whereas the local time is affected by the timezone setting.  It's a common question and it's all made clear in the Experts-Exchange article about dates and times.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
umaxim

ASKER
i read this article 2 time already and can not find the reason why my script give me 1 hour late time. I add new york zone to server and same zone to quickbooks program.  Then I add same setting to my script

// We need to make sure the correct timezone is set, or some PHP installations will complain
if (function_exists('date_default_timezone_set'))
{
      date_default_timezone_set('America/New_York');
}

I have exact same time on both servers. But still program return me extra hour
Ray Paseur

Why are you wrapping the function call in an if() statement?

What is the value of date_default_timezone_get() or the value displayed in phpinfo():
umaxim

ASKER
date

date/time support      enabled
"Olson" Timezone Database Version      0.system
Timezone Database      internal
Default timezone      America/New_York
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Ray Paseur

Wondering about the Timezone Database Version.  Could it be stuck in daylight savings time?  Here is what mine says:

date/time support       enabled
"Olson" Timezone Database Version       2013.8
Timezone Database       internal
Default timezone       America/Chicago
umaxim

ASKER
i checked script it only add extra hour when
2013-10-06T20:38:00-05:00
            2013-10-06 21:38:00

and when
           
2013-12-09T11:22:20-05:00
2013-12-09 11:22:20
Ray Paseur

The first date is during daylight savings time.  The second date is not.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
umaxim

ASKER
ok but how i can fix this kind of problem any idea ?
Ray Paseur

Please see: http://www.laprbass.com/RAY_temp_umaxim.php

The timezone offset from GMT is the same on both date strings, but it should be an hour different when daylight saving time is in play.  That may not be the only issue, but it could be the problem.  Since the datetime string specified the exact hours of the offset, PHP probably took the value and used it "as is."

<?php // RAY_temp_umaxim.php
error_reporting(E_ALL);
echo '<pre>';

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28338819.html

// MAKE SURE THE TIMEZONE IS SET CORRECTLY
date_default_timezone_set('America/New_york');
echo PHP_EOL . date_default_timezone_get();

// SHOW THE TIMEZONE OFFSETS FOR NOT-DST AND FOR DST
echo PHP_EOL . date('c', strtotime('Jan 1, 2013'));
echo PHP_EOL . date('c', strtotime('Jul 1, 2013'));

$date_1 = '2013-10-06T20:38:00-05:00';
$ts_1   = strtotime($date_1);

$date_2 = '2013-12-09T11:22:20-05:00';
$ts_2   = strtotime($date_2);

// CHECK DAYLIGHT SAVING TIME
echo PHP_EOL . "$date_1 SETTING FOR DST IS: " . date('I', $ts_1);
echo PHP_EOL . "$date_2 SETTING FOR DST IS: " . date('I', $ts_2);

Open in new window

HTH, ~Ray
ASKER CERTIFIED SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
umaxim

ASKER
Thank you
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Ray Paseur

Thanks for the points, and thanks for using EE, ~Ray