Link to home
Start Free TrialLog in
Avatar of glynco
glynco

asked on

php time convert to friendly

I have this
$displayDateTime="2012-04-03-17:06:52";
echo date("jS \of F Y", strtotime($displayDateTime));

Open in new window


Result is
1st of January 1970
Avatar of Loganathan Natarajan
Loganathan Natarajan
Flag of India image

what format do want to have?
if you need  dd/mm/yyyy

try echo date("d/m/Y");
if you need m/d/y, try like this,

<?php
echo date("m/d/y");
?>
The issue here is that you have got "almost" an ISO-8601 DATETIME string with this: 2012-04-03-17:06:52  If it said 2012-04-03T17:06:52 your code would work.  So my question would be, "Where did that string come from?"  If your code is generating it, change the code to generate something in the date('c') format.  If you are getting it from an external source, ask the data provider to correct the data so that it complies with ISO-8601 standards.  If the data provider is unable to do that you can try replacing the rightmost hyphen with the letter 'T' and you should be OK.
I think the example you have given has some problem

$displayDateTime="2012-04-03-17:06:52";

what does 04-03 stands for? That is why it is showing as 1st of January 1970
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rinfo
rinfo

As Ray has commented
1. There has to be a required default time zone declaration for php version above 5.1
2. $displayDateTime="2012-04-03-17:06:52"; is being evaluated to  0 resulting in
    1st of January 1970 as date.
3. Problem is with the $displayDateTime which receives the value of the string supposed to be
    converted into datetime.
4. $displayDateTime = "03-04-2012 17:06:52" should result in correct evalutation of datetime;
5.echo date("jS \of F Y", strtotime($displayDateTime));
This following simple code has resulted in
<?php
   date_default_timezone_set('America/Chicago');
   $displayDateTime = "03-04-2012 17:06:52";
   echo date("jS \of F Y", strtotime($displayDateTime));
?>
result as 3rd of April 2012