Solved

Format a date in PHP

Posted on 2012-04-04
10
274 Views
Last Modified: 2012-04-22
$Date = "040312"; //mmddyy

I want the date to be formatted as such: 4/3/2012, how can I do that?
0
Comment
Question by:Computer Guy
10 Comments
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 37807879
<?php
$Date = '040312';
$DateTimeObject = DateTime::createFromFormat('mdy', $Date);
echo $DateTimeObject->format('n/j/Y');
?>

Open in new window

Bye, Olaf.
0
 
LVL 3

Author Comment

by:Computer Guy
ID: 37807975
Hi,

Thanks, but I got this error:

Fatal error: Call to undefined method DateTime::createFromFormat() in /home/test/public_html/datetest.php on line 3
0
 
LVL 29

Accepted Solution

by:
Olaf Doschke earned 250 total points
ID: 37808221
What version of php are you using?

DateTime::createFromFormat()  exists since version 5.3.0

Bye, Olaf.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37808822
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 108

Assisted Solution

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

date_default_timezone_set('America/Chicago');

// NEVER STORE A DATE IN THIS FORMAT - USE ISO-8601 INSTEAD
$date = '040312';

// REFORMAT THE DATE
$new
= substr($date,0,2)
. '/'
. substr($date,2,2)
. '/'
. substr($date,4,2)
;
echo $new;
echo PHP_EOL;

// CONVERT TO ISO-8601
$iso = date('c', strtotime($new));
echo $iso;
echo PHP_EOL;

// CONVERT TO ANOTHER FORMAT
$alt = date('n/j/Y', strtotime($new));
echo $alt;
echo PHP_EOL;

Open in new window

0
 
LVL 9

Expert Comment

by:rinfo
ID: 37810427
This can be done using following code.
$Date = "040312";
$m  =  substr($date,0,2);
$d =   substr($date,2,2);
$y =    substr($date,4,2);
$uDate  = mktime(0, 0, 0, $m, $d, $y);
$fdate   =  date("m/d/y", $udate);

However end point is what Ray has to say . Stick to convention always.
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 37846023
I asume audiotech520 has updated PHP so my first answer works, but then that answer should be marked, not the (indirect) advice to update PHP.

It would be fair and I would not mind, if points would be shared on several answers, which also work with older PHP versions.

Bye, Olaf.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37860179
This worked when I tested it: ID: 37808833.  Not particularly worried about the points, just keeping an accurate answer set in the PAQ.
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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
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.

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

13 Experts available now in Live!

Get 1:1 Help Now