Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP Date issue

Posted on 2014-03-06
35
Medium Priority
?
495 Views
Last Modified: 2014-03-11
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
Comment
Question by:classnet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 9
  • 7
  • +1
35 Comments
 
LVL 10

Expert Comment

by:aboo_s
ID: 39909006
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39909029
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
 

Author Comment

by:classnet
ID: 39909047
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39909061
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
 

Author Comment

by:classnet
ID: 39909069
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
 
LVL 59

Expert Comment

by:Julian Hansen
ID: 39909075
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
 
LVL 59

Expert Comment

by:Julian Hansen
ID: 39909089
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
 

Author Comment

by:classnet
ID: 39909104
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
 
LVL 10

Accepted Solution

by:
aboo_s earned 2000 total points
ID: 39909121
<?php echo date('M/d/Y',strtotime($_POST["theDate"])) ?>
0
 
LVL 59

Expert Comment

by:Julian Hansen
ID: 39909136
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
 

Author Comment

by:classnet
ID: 39909138
That is what I have been using...

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

This yields Jun/03/2014
0
 
LVL 10

Expert Comment

by:aboo_s
ID: 39909144
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
 
LVL 10

Expert Comment

by:aboo_s
ID: 39909152
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
 
LVL 59

Expert Comment

by:Julian Hansen
ID: 39909163
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39909182
This doesn't need to be a mystery or point of confusion.  It's all in the article.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39909207
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
 

Author Comment

by:classnet
ID: 39909737
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
 
LVL 59

Expert Comment

by:Julian Hansen
ID: 39909867
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39909920
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
 

Author Comment

by:classnet
ID: 39909998
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39910063
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
 
LVL 59

Expert Comment

by:Julian Hansen
ID: 39910112
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
 

Author Comment

by:classnet
ID: 39910124
Yes julianH... you mentioned that at 08:18:55.
0
 
LVL 10

Expert Comment

by:aboo_s
ID: 39911534
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
 

Author Comment

by:classnet
ID: 39917256
Thanks.... this lead to the solution.
0
 
LVL 10

Expert Comment

by:aboo_s
ID: 39917270
I'm sorry, I'm just not quite sure what the solution was.\
Could you kindly explain?
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39917311
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
 
LVL 10

Expert Comment

by:aboo_s
ID: 39917618
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
 

Author Comment

by:classnet
ID: 39917626
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
 
LVL 10

Expert Comment

by:aboo_s
ID: 39917638
Thank you for the acknowledgment.
Even if this doesn't get fixed, your acknowledging it would be satisfactory.
0
 

Author Comment

by:classnet
ID: 39917659
Sorry about the mess up aboo_s.  I get some attention on this.
0
 
LVL 59

Expert Comment

by:Julian Hansen
ID: 39917664
How do I fix?
Click the Request Attention link underneath your original post and ask a mod to re-open the question.
0
 
LVL 10

Expert Comment

by:aboo_s
ID: 39917677
Thanks
0
 

Author Closing Comment

by:classnet
ID: 39919949
Thank you aboo_s
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

636 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