0

I'm about to pull my hair out people. I've got a form where people select a day and a month from the dropdown menu, and the current year would automatically get concatenated.

On the server I've got this:

$received =  date("Y")."-".$_POST['month']."-".$_POST['day'];
$date = strtotime($received);

$newformat = date('Y F j',$date);
echo $newformat;

Let's say $_POST['month'] == 'April' and $_POST['day'] == '5'

The output always comes out like this:

2013 April 1 (ie the day defaults to 1).

Interestingly if $_POST['day'] == '24' then the output is:

2013 April 2

It's as though it just looks at the first digit.. Any ideas would be greatly appreciated!

7
  • Have you verified what $received contains? Commented Sep 7, 2013 at 1:42
  • possible duplicate of String to date conversion in PHP Commented Sep 7, 2013 at 1:42
  • Yes - if I do echo $received; I'll get 2013-April-5 - correct output.. Commented Sep 7, 2013 at 1:43
  • @remyabel - it makes no difference. If I do $newformat = date('Y-F-j',$date); I get the same thing - 2013-April-1 Commented Sep 7, 2013 at 1:46
  • Drop the dashed in the $received format and see if that helps. Commented Sep 7, 2013 at 1:47

2 Answers 2

2

Is your issue that your "Month" variable is a string instead of an integer, but you are putting it in the date as if it's an integer? 2013-April-5... Try just putting in a 4 instead of April.

Update

To answer your question, you can have your client updated to have options like <option value="1">January</option>, or update your PHP to process it like:

<? $received =  $_POST['month']." ".$_POST['day']." ".date("Y");
$date = strtotime($received);

$newformat = date('Y F j',$date);
echo $newformat; ?>

I think that will work.

Sign up to request clarification or add additional context in comments.

4 Comments

I think you're right! On the client side the dropdown shows the month as a string - January, February, etc. When the form gets submitted $_POST['month'] is the value of the dropdown which comes in the form of a string. Is there any way to convert that into a date without having to convert the month to an integer?
@user1775598 Updated my answer to help.
Exactly what I needed thanks! Was the problem that I set $received var with dashes inside or was it the order in which I month, day and year came? Or both?
@user1775598 I'd say it was an issue of both. Dashes wouldn't be used when spelling out the month so PHP wouldn't understand the format. Glad I could help though, and don't forget to mark this as the answer if it fixed your problem for future visitors. Thanks~
1

The problem may lie in your handler and/or your form (without seeing full code in your question).

I've tested it and I get back perfect results.

When I entered 5 for month and 24 for day in the form I build below, I get back 2013 May 24.

Check your syntax in your form and your handler.

In using the following form and handler, I get perfect results

HTML form

<form action="date_handler.php" method="post">
Month: <input type="text" name="month">
<br>
Day: <input type="text" name="day">
<br>
<input type="submit" name="submit" value="Submit">
</form>

PHP handler

<?php
$month=$_POST['month'];
$day=$_POST['day'];
$received =  date("Y")."-".$_POST['month']."-".$_POST['day'];
$date = strtotime($received);
$newformat = date('Y F j',$date);
echo $newformat;
?>

2 Comments

Thanks man. I think the problem is that the $_POST['month'] var was a being parsed as a string instead of an int. Now I'm trying to figure out if it's possible to create a date using a string for the month so that I don't have to convert it to an int.
@user1775598 You're welcome. Can you post your full code? Maybe there's something in there that's breaking it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.