Solved

Format a date in PHP

Posted on 2012-04-04
10
291 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
[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
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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 37808822
0
 
LVL 110

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 110

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

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

624 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