Page 1 of 1

Day Of Week - showing date for invalid date??

Posted: Fri Apr 27, 2012 4:40 am
by simonmlewis

Code: Select all

$diaryday = $_POST['diaryday'];
$diarymth = $_POST['diarymth'];
$diaryyear = $_POST['diaryyear'];

$diarydate = "$diaryyear-$diarymth-$diaryday";
$h = mktime(0, 0, 0, $diarymth, $diaryday, $diaryyear);
$d = date("F dS, Y", $h) ;

// CHECKS WHAT DAY OF WEEK DIARY SELECTION IS
$w= date("l", $h) ;
$w is meant to capture the date selected and show the day of week.
However, if I select April 31st, 2012 (which doesn't exist!), it shows it as a Tuesday.

How is this possible?? Is PHP not working right?

Re: Day Of Week - showing date for invalid date??

Posted: Fri Apr 27, 2012 8:19 am
by mikosiko
clearly documented in the manual
http://php.net/manual/en/function.mktime.php
day
The number of the day relative to the end of the previous month. Values 1 to 28, 29, 30 or 31 (depending upon the month) reference the normal days in the relevant month. Values less than 1 (including negative values) reference the days in the previous month, so 0 is the last day of the previous month, -1 is the day before that, etc. Values greater than the number of days in the relevant month reference the appropriate day in the following month(s).
May 01 is Tuesday

Re: Day Of Week - showing date for invalid date??

Posted: Fri Apr 27, 2012 8:24 am
by simonmlewis
So it can be wrong?

Re: Day Of Week - showing date for invalid date??

Posted: Fri Apr 27, 2012 8:33 am
by Celauran
PHP Manual wrote:The number of the day relative to the end of the previous month. Values 1 to 28, 29, 30 or 31 (depending upon the month) reference the normal days in the relevant month. Values less than 1 (including negative values) reference the days in the previous month, so 0 is the last day of the previous month, -1 is the day before that, etc. Values greater than the number of days in the relevant month reference the appropriate day in the following month(s).
It's working as intended.

Re: Day Of Week - showing date for invalid date??

Posted: Fri Apr 27, 2012 8:36 am
by simonmlewis
In this case, incorrectly.
I'm telling it the date, and it's telling me the day - even tho the date does not exist.

Re: Day Of Week - showing date for invalid date??

Posted: Fri Apr 27, 2012 8:42 am
by mikosiko
simonmlewis wrote:In this case, incorrectly.
I'm telling it the date, and it's telling me the day - even tho the date does not exist.
no is not... you are not "telling it the date"... you are "telling it" a TIMESTAMP that was calculated correctly (with your invalid data)for the function mktime ...

Re: Day Of Week - showing date for invalid date??

Posted: Fri Apr 27, 2012 8:48 am
by simonmlewis
So the way PHP/Apache works out the date, is not based on "real dates" then?

Re: Day Of Week - showing date for invalid date??

Posted: Fri Apr 27, 2012 8:53 am
by simonmlewis
Bingo.
Instead, once posted I am checking if they are submitting "31" and if they are, checking if it is the months that never have a 31st, and thus providing error message.

I just assumed PHP knew dates!!

Re: Day Of Week - showing date for invalid date??

Posted: Fri Apr 27, 2012 8:53 am
by Celauran
Sure it is. You aren't working with dates, you're working with timestamps.

Re: Day Of Week - showing date for invalid date??

Posted: Fri Apr 27, 2012 8:57 am
by simonmlewis
So when I am using that script, and asking it what date of week 2012-04-31 is, it doesn't read that as a date? Even tho it's giving day of week (which is part of a date)?

At least I found a way around it.

Code: Select all

  if ($diaryday == "31")
  { if ($diarymth == "09" || $diarymth == "04" || $diarymth == "06" || $diarymth == "11") { echo "&nbsp; <b><font color='#ff0000'>Sorry this is an invalid date</font></b>";
  $diaryyear = NULL;
  $diaryday = NULL;}
  }

Re: Day Of Week - showing date for invalid date??

Posted: Fri Apr 27, 2012 12:08 pm
by x_mutatis_mutandis_x
simonmlewis wrote:So when I am using that script, and asking it what date of week 2012-04-31 is, it doesn't read that as a date? Even tho it's giving day of week (which is part of a date)?

At least I found a way around it.

Code: Select all

  if ($diaryday == "31")
  { if ($diarymth == "09" || $diarymth == "04" || $diarymth == "06" || $diarymth == "11") { echo "&nbsp; <b><font color='#ff0000'>Sorry this is an invalid date</font></b>";
  $diaryyear = NULL;
  $diaryday = NULL;}
  }
Show a mini-calendar and have users to select the date based on it, and make your text field read only. The javascript calendar validates the proper date for you, and all you have to do is make sure its a valid format and you are done.

Re: Day Of Week - showing date for invalid date??

Posted: Fri Apr 27, 2012 12:20 pm
by pickle
Don't rely on javascript to validate the date. That's client side and can't be trusted.

Server side, just use http://ca.php.net/manual/en/function.checkdate.php

Re: Day Of Week - showing date for invalid date??

Posted: Fri Apr 27, 2012 12:25 pm
by x_mutatis_mutandis_x
pickle wrote:Don't rely on javascript to validate the date. That's client side and can't be trusted.

Server side, just use http://ca.php.net/manual/en/function.checkdate.php
Thanks for the save! Couldn;t think of the function on top of my head, I guess checkdate() also tells you if the day is a valid one for a given month and a year.