?
Solved

php time convert to friendly

Posted on 2012-04-03
9
Medium Priority
?
258 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

 
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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

801 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