-3

Is it possible through PHP to change the format of this date

Jan 1 1900 12:00AM

to

01/01/1990

mm/dd/yyy

Somk
  • 11,869
  • 32
  • 97
  • 143
  • 1
    RTFM (I recently learned this acronym and I LOVE IT... It applies to so many questions here) – Ethan Jun 06 '13 at 18:26
  • 1
    I assume you mean "Jan 1 1990" in the first line, and not 1900. – matt Jun 06 '13 at 18:26
  • Why are you interested in this wonky date format? It might mislead the readers. One can't say for sure whether it is in the `mm/dd/yyyy` or `dd/mm/yyyy` format. – Lion Jun 06 '13 at 18:30
  • @Lion if he is only using it in the U.S. it would make sense because most people assume that format here. – Ethan Jun 06 '13 at 18:51

6 Answers6

3

Two functions will help: strtotime and date.

So do this:

date("m/d/Y", strtotime("Jan 1 1990 12:00AM"))
matt
  • 1,947
  • 1
  • 19
  • 29
1

See PHP's excellent DateTime classes:-

$date = DateTime::createFromFormat('M d Y H:ia', 'Jan 1 1900 12:00AM');
echo $date->format('m/d/Y');

Or, if you are a fan of one liners:-

echo DateTime::createFromFormat('M d Y H:ia', 'Jan 1 1900 12:00AM')->format('m/d/Y');
vascowhite
  • 18,120
  • 9
  • 61
  • 77
  • I never knew about `DateTime::createFromFormat`... will definitely use this in the future. – Ethan Jun 06 '13 at 18:52
  • @Ethan Once you start using those classes you'll realise how powerful they are. They make [seemingly complex problems](http://stackoverflow.com/q/16712277/212940) suddenly [very simple](http://stackoverflow.com/a/16712661/212940). – vascowhite Jun 06 '13 at 18:59
  • I've used DateTime before, just never that method. Any idea if there is any simpler way to do what I did [here](https://gist.github.com/etaubman/5609636)? – Ethan Jun 06 '13 at 20:08
  • @Ethan I'm not sure it's any simpler https://github.com/vascowhite/TimeAgo/blob/master/TimeAgo.php – vascowhite Jun 06 '13 at 21:10
0

You could parse that into a timestamp with strtotime() and then use the PHP date() function to format it.

Mike
  • 2,132
  • 3
  • 20
  • 33
0

strtotime and strftime are the easiest to work with.

$stamp = strtotime('Jan 1 1900 12:00AM');
echo strftime("%m/%d/%G", $stamp);

See the full strftime documentation to output your date in any format.

jterry
  • 6,209
  • 2
  • 30
  • 36
0

No, because Jan 1 1900 1200AM is never going to equal 01/01/1990. Check you year. But if you want a simple what to convert string to time try this:

<?php
$d = 'Jan 1, 1900 12:00AM';
echo date("m/d/y",strtotime($d));
?>

I would pass this into a function first that removed the time element and then converts only the data portion.

AJames
  • 64
  • 3
-1

see the date function:

//replace the time() with yours
echo date('m/d/Y', time()); // = 06/06/2013

manual: http://php.net/manual/de/function.date.php

sjkm
  • 3,887
  • 2
  • 25
  • 43