Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

php time convert to friendly

Posted on 2012-04-03
9
Medium Priority
?
262 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 111

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

824 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