Solved

php time convert to friendly

Posted on 2012-04-03
9
251 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37800632
0
 
LVL 108

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 108

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now