• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 498
  • Last Modified:

PHP Date issue

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?
0
classnet
Asked:
classnet
  • 11
  • 9
  • 7
  • +1
1 Solution
 
aboo_sCommented:
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.
0
 
Ray PaseurCommented:
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.
0
 
classnetAuthor 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"])) ?>
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Ray PaseurCommented:
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()) ?>>
0
 
classnetAuthor 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?
0
 
Julian HansenCommented:
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)
0
 
Julian HansenCommented:
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

0
 
classnetAuthor 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.
0
 
aboo_sCommented:
<?php echo date('M/d/Y',strtotime($_POST["theDate"])) ?>
0
 
Julian HansenCommented:
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

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

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

This yields Jun/03/2014
0
 
aboo_sCommented:
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!
0
 
aboo_sCommented:
This as well to be changed:
Date <input style='text-align=center' type=text name=theDate value=<?php echo date('m/d/Y',time()) ?>>
0
 
Julian HansenCommented:
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?
0
 
Ray PaseurCommented:
This doesn't need to be a mystery or point of confusion.  It's all in the article.
0
 
Ray PaseurCommented:
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
0
 
classnetAuthor 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.
0
 
Julian HansenCommented:
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
0
 
Ray PaseurCommented:
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.
0
 
classnetAuthor 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.
0
 
Ray PaseurCommented:
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.
0
 
Julian HansenCommented:
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.
0
 
classnetAuthor Commented:
Yes julianH... you mentioned that at 08:18:55.
0
 
aboo_sCommented:
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!
0
 
classnetAuthor Commented:
Thanks.... this lead to the solution.
0
 
aboo_sCommented:
I'm sorry, I'm just not quite sure what the solution was.\
Could you kindly explain?
0
 
Ray PaseurCommented:
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!
0
 
aboo_sCommented:
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!
0
 
classnetAuthor 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?
0
 
aboo_sCommented:
Thank you for the acknowledgment.
Even if this doesn't get fixed, your acknowledging it would be satisfactory.
0
 
classnetAuthor Commented:
Sorry about the mess up aboo_s.  I get some attention on this.
0
 
Julian HansenCommented:
How do I fix?
Click the Request Attention link underneath your original post and ask a mod to re-open the question.
0
 
aboo_sCommented:
Thanks
0
 
classnetAuthor Commented:
Thank you aboo_s
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 11
  • 9
  • 7
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now