PHP Date issue

Sheldon Livingston
Sheldon Livingston used Ask the Experts™
on
On one page I have an input box as such:

Date <input style='text-align=center' type=text name=theDate value=<?php echo date('m-d-Y',time()) ?>>

The page shows the date, on March 6 (today), as 03-06-2014

There is a submit button that brings you to another page.

The other page has this code:

<?php echo date('M-d-Y',strtotime($_POST["theDate"])) ?>

The date displays as Jun-03-2014

Any thoughts on why the month and day would switch?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
It's the strtotime function, it had no indication to which is the month and which is the day
so the default was d-m-Y , unless you specify otherwise.
Most Valuable Expert 2011
Top Expert 2016

Commented:
Please read this article, then post back if you still have questions:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

Executive summary: All internal representations of the DATETIME values should follow the ISO-8601 standard.  The standard removes the ambiguity of month and day.
Sheldon LivingstonConsultant

Author

Commented:
aboo_s... let's say I want page 2 to display the date as Mar-06-2014... how is that accomplished?

How would I alter this code:

<?php echo date('M-d-Y',strtotime($_POST["theDate"])) ?>
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Most Valuable Expert 2011
Top Expert 2016

Commented:
You cannot make the change there; the script already "munged" the value.  This needs to be changed:

Date <input style='text-align=center' type=text name=theDate value=<?php echo date('m-d-Y',time()) ?>>

You might try using the ISO-8601 date representation, by changing the date pattern in that line to this:

Date <input style='text-align=center' type=text name=theDate value=<?php echo date('Y-m-d',time()) ?>>
Sheldon LivingstonConsultant

Author

Commented:
All that I am trying to do here is display a date that a user can change.  

On page one I supply todays date.  I want the user to be able to enter 05-28-1962 to indicate that they were born on May 28, 1962.  I would like them to submit the page and have the other page read May 28, 1962.

Is this possilbe?
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Lets put the date output lines together

<?php echo date('m-d-Y',time()) ?>
<?php echo date('M-d-Y',strtotime($_POST["theDate"])) ?>

Can you see the difference?

The first parameter to the date function is the format for the date and each letter means something different. The format is case sensitve so a capital M means something different from a lower case m.

m - means output month as a zero padded 2 digit number 01 -12
M - means ouptut the abbreviated month name Jan - Dec
F - means output the full name January - December

You can read more about what the different format symbols mean here (http://lv1.php.net/manual/en/function.date.php)
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
You would then use the strtotime function.

If the user submits the page with the date in field theDate you could do something like this

$theDate = date('M-d-Y', strtotime($_POST['theDate']));

Open in new window


$theDate will now be a string with the value set as 'Mar-06-2014'

However, this is only useful as an output format - the storing of dates is not dependent on format - you only format the date when you want to display it - if you are going to be using the date internally - say to store in a Database you would simply do
$theDate = strtotime($_POST['theDate']);

Open in new window

Sheldon LivingstonConsultant

Author

Commented:
julianH:  I know the m vs M thing.  I am showing a form using m-d-Y and defaulting today's date to display 03-06-2014.

When the user submits the form, I simply want the resultant page to display Mar-06-2014.

If the user changes the date to read 03-05-2014 then I would like the posted page to read Mar-05-2014.

If they did change the date to 03-05-2014 they would be doing this on page 1 in the theDate input box... thus I was trying to shape the display of the $_POST["theDate"] element.
<?php echo date('M/d/Y',strtotime($_POST["theDate"])) ?>
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
And the problem is ?

I am not with you on this if you want to output the date as you have described then what is wrong with what youare doing ?
<?php echo date('M-d-Y',strtotime($_POST["theDate"])) ?> 

Open in new window

Sheldon LivingstonConsultant

Author

Commented:
That is what I have been using...

<?php echo date('M/d/Y',strtotime($_POST["theDate"])) ?>

This yields Jun/03/2014
Oh I see:
it's a matter of formatting, usually in Europe they use D-M-Y
while in America they use M/D/Y so that's how strtotime understands it!
// means american hence M/D/Y and not D/M/Y
lame I agree, but comprehensible!
This as well to be changed:
Date <input style='text-align=center' type=text name=theDate value=<?php echo date('m/d/Y',time()) ?>>
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
If you use '/' then it is interpreted as a US date format '-' is for UTC dates.

Re converting the second part - what is the issue there - just use the same rules as before?
Most Valuable Expert 2011
Top Expert 2016

Commented:
This doesn't need to be a mystery or point of confusion.  It's all in the article.
Most Valuable Expert 2011
Top Expert 2016

Commented:
Man page: http://php.net/manual/en/datetime.formats.date.php

Code sample: http://www.iconoun.com/demo/temp_classnet.php

<?php // demo/temp_classnet.php
error_reporting(E_ALL);

// SET THE TIMEZONE
date_default_timezone_set('America/Chicago');

// NOTE Time() IS REDUNDANT IN date()
$d = date('m-d-Y',time());

// MAKE A TIMESTAMP AND A NEW DATE STRING
$t = strtotime($d);
$n = date('M-d-Y', $t);

// SHOW THE DATA
echo PHP_EOL . "<br>ORIGINAL $d CONVERTS INCORRECTLY INTO $n";


// NOW DO IT RIGHT USING THE ISO-8601 FORMAT
// NOTE Time() IS REDUNDANT IN date()
$d = date('Y-m-d',time());

// MAKE A TIMESTAMP AND A NEW DATE STRING
$t = strtotime($d);
$n = date('M-d-Y', $t);

// SHOW THE DATA
echo PHP_EOL . "<br>ORIGINAL $d CONVERTS CORRECTLY INTO $n";


// NOW DO IT RIGHT AGAIN USING SLASHES INSTEAD OF DASHES
// NOTE Time() IS REDUNDANT IN date()
$d = date('m/d/Y',time());

// MAKE A TIMESTAMP AND A NEW DATE STRING
$t = strtotime($d);
$n = date('M-d-Y', $t);

// SHOW THE DATA
echo PHP_EOL . "<br>ORIGINAL $d CONVERTS CORRECTLY INTO $n";

Open in new window

HTH, ~Ray
Sheldon LivingstonConsultant

Author

Commented:
This is funny... it doesn't need to be a mystery but it is.

I'd think that this could be answered in 15 lines of code or less.

Page 1 contains an input box and someone types in 03/05/2014 and they mean this to represent March 5th of 2014.  They mash a submit button and it is desired that page 2 shows Mar 5 2014.

The code below does not work:

date('M/d/Y',strtotime($_POST["theDate"]))

The code above displays Jun/03/2014

It's a mystery to me.
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
This works for me
<!doctype html>
<html>
<head>
<title>Test</title>
</head>
<body>
  <form action="t587.php" method="post">
    Date <input type="text" name="theDate" />
    <input type="submit" />
  </div>
</body>
</html>

Open in new window

PHP File t587.php
<?php
$date = empty($_POST['theDate']) ? time() : strtotime ( $_POST['theDate'] ) ;
$strdate = date('M/d/Y', $date);
?>
<!doctype html>
<html>
<head>
<title>Test</title>
<style type="text/css">
b {
	font-size: 20px;
}
</style>
</head>
<body>
The date is <b><?php echo $strdate;?></b>
</body>
</html>

Open in new window


Enter 05/03/2014 and the form and the script prints out Mar/05/2014
Enter 2014-03-05 and the form spits out the same
Most Valuable Expert 2011
Top Expert 2016

Commented:
Did you read this man page yet?  If not, please take a moment to read it so you will understand what PHP is doing with the various forms of the date string.
http://php.net/manual/en/datetime.formats.date.php

The matter of sending the date string through an HTML form has nothing to do with the issue (and may contribute to the confusion).  The only thing that is in play here is the format of the date string and the way that PHP strtotime() and related functions convert the date string into the UNIX timestamp.

All internal representations of a date/time value should be carried in the ISO-8601 format.  "Pretty dates" are formatted for a particular purpose, for example, to use in a business letter.  These are formatted according to the context that is appropriate.  The only appropriate internal context is an unambiguous date, and for that reason we use ISO-8601 standards.
Sheldon LivingstonConsultant

Author

Commented:
Ray... I did read that page...

So, it turns out that if the input box contains hyphens 3-5-2014 the result is May 3... 3/5/2014 yields Mar 5.
Most Valuable Expert 2011
Top Expert 2016

Commented:
Yes, I think you interpret it correctly.  PHP had to disambiguate strings like month-day-year versus day-month-year.  So they made an arbitrary decision about the meaning of the hyphen and the slash.  But if you list the year first, there is a consistent interpretation of the next two fields according to the ISO8601 standard.
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
So, it turns out that if the input box contains hyphens 3-5-2014 the result is May 3... 3/5/2014 yields Mar 5.
I  think we established that a couple posts back.
Sheldon LivingstonConsultant

Author

Commented:
Yes julianH... you mentioned that at 08:18:55.
I'm sorry, but did you try this code:
Date <input style='text-align=center' type=text name=theDate value=<?php echo date('m-d-Y',time()) ?>>
<?php echo date('M-d-Y',strtotime($_POST["theDate"])) ?>
where m-d-Y was altered to m/d/Y
and M-d-Y was changed to M/d/Y ?  did you try it ? to be clear on this!
If the answer is yes then I have crazy solution. Altheough I beleive the code above has to work!
Sheldon LivingstonConsultant

Author

Commented:
Thanks.... this lead to the solution.
I'm sorry, I'm just not quite sure what the solution was.\
Could you kindly explain?
Most Valuable Expert 2011
Top Expert 2016

Commented:
For anyone who is not a computer scientist and finds themselves looking at this question, the correct answer is to be found here:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28381720.html#a39909061

There is a reason why the ISO created the standard for DATETIME representations, and this question is the dictionary picture of what happens when you ignore the standards!
It surely does not take a scientist to follow simple instruction, which I have provided all the way before the accepted solution, yet I have been cut out! Which needs an explanation of some kind!
Sheldon LivingstonConsultant

Author

Commented:
aboo_s is correct in that his comment, which I overlooked, should have been the accepted solution as it was posted prior to the currently excepted solution.

How do I fix?
Thank you for the acknowledgment.
Even if this doesn't get fixed, your acknowledging it would be satisfactory.
Sheldon LivingstonConsultant

Author

Commented:
Sorry about the mess up aboo_s.  I get some attention on this.
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
How do I fix?
Click the Request Attention link underneath your original post and ask a mod to re-open the question.
Sheldon LivingstonConsultant

Author

Commented:
Thank you aboo_s

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial