-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

4
  • 1
    RTFM (I recently learned this acronym and I LOVE IT... It applies to so many questions here) Commented Jun 6, 2013 at 18:26
  • 1
    I assume you mean "Jan 1 1990" in the first line, and not 1900. Commented Jun 6, 2013 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. Commented Jun 6, 2013 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. Commented Jun 6, 2013 at 18:51

6 Answers 6

3

Two functions will help: strtotime and date.

So do this:

date("m/d/Y", strtotime("Jan 1 1990 12:00AM"))
Sign up to request clarification or add additional context in comments.

Comments

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');

4 Comments

I never knew about DateTime::createFromFormat... will definitely use this in the future.
@Ethan Once you start using those classes you'll realise how powerful they are. They make seemingly complex problems suddenly very simple.
I've used DateTime before, just never that method. Any idea if there is any simpler way to do what I did here?
@Ethan I'm not sure it's any simpler github.com/vascowhite/TimeAgo/blob/master/TimeAgo.php
0

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

Comments

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.

Comments

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.

Comments

-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

2 Comments

please explain down vote
I didn't downvote, but I assume it's because you didn't actually take the input date and parse it. You just didn't the output format part.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.