Solved

php time convert to friendly

Posted on 2012-04-03
9
254 Views
Last Modified: 2012-04-07
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
0
Comment
Question by:glynco
9 Comments
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 37800434
what format do want to have?
0
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 37800438
if you need  dd/mm/yyyy

try echo date("d/m/Y");
0
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 37800444
if you need m/d/y, try like this,

<?php
echo date("m/d/y");
?>
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 37800466
Otherwise, try with date_parse_from_format

ref. http://de3.php.net/manual/en/function.date-parse-from-format.php
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 37800632
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 37800652
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.
0
 
LVL 5

Expert Comment

by:Pramod Nair
ID: 37800676
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
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 37800687
See http://www.laprbass.com/RAY_temp_glynco.php
<?php // RAY_temp_glynco.php
error_reporting(E_ALL);
echo "<pre>";


// REQUIRED FOR PHP 5.1+
date_default_timezone_set('America/Chicago');

// A BAD DATE STRING
$ugly = '2012-04-03-17:06:52';
if (!$timestamp = strtotime($ugly)) echo "$ugly DOES NOT COMPLY WITH ISO STANDARDS";
echo PHP_EOL;

// REPLACE RIGHTMOST HYPHEN
$lump = explode('-', $ugly);
$good
= $lump[0]
. '-'
. $lump[1]
. '-'
. $lump[2]
. 'T'
. $lump[3]
;

if ($timestamp = strtotime($good)) echo "$good COMPLIES WITH ISO STANDARDS";
echo PHP_EOL;
echo date("jS \of F Y", $timestamp);

Open in new window

0
 
LVL 9

Expert Comment

by:rinfo
ID: 37805469
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
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

861 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