Solved

PHP: Convert date format

Posted on 2011-03-09
5
337 Views
Last Modified: 2012-05-11
Using PHP, how can I change this:
2011/03/16 08:00

to this:
03-16-2011
?
0
Comment
Question by:hankknight
  • 3
  • 2
5 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35086112
Like so

$d = "2011/03/16 08:00";

echo date("m-d-Y", strtotime($d) );
0
 
LVL 27

Expert Comment

by:yodercm
ID: 35086164
Be sure to check dates such as 2011/03/05 and 2011/05/03.  strtotime() sometimes has a hard time distinguishing which format the incoming date is in.

If strtotime() doesn't get yours right, then you can pull out each piece of the date with substr().
0
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 200 total points
ID: 35086250
If there are problems as Yodercm suggests, another choice would be to convert it to exif format which strtotime definitely supports

EXIF       YY ":" MM ":" DD " " HH ":" II ":" SS       "2008:08:07 18:11:31"

http://uk.php.net/manual/en/datetime.formats.compound.php

$d = str_replace('/', ':', "2011/03/16 08:00");
0
 
LVL 27

Accepted Solution

by:
yodercm earned 300 total points
ID: 35086315
Here is the simplest way:


$olddate = '2011/03/16 08:00';

$year=substr($olddate,0,4);
$month=substr($olddate,5,2);
$day=substr($olddate,8,2);

$newdate=$month."-".$day."-".$year;

echo $newdate;
0
 
LVL 27

Expert Comment

by:yodercm
ID: 35086330
Or, if you are a fan of conciseness,

$olddate = '2011/03/16 08:00';
$newdate=substr($olddate,5,2)."-".substr($olddate,8,2)."-".$year=substr($olddate,0,4);
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

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 …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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.

707 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

14 Experts available now in Live!

Get 1:1 Help Now