Solved

php time convert to friendly

Posted on 2012-04-03
9
253 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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…
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

809 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