Day Of Week - showing date for invalid date??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Day Of Week - showing date for invalid date??

Post 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?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

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

Post 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
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post by simonmlewis »

So it can be wrong?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

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

Post 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 ...
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post by simonmlewis »

So the way PHP/Apache works out the date, is not based on "real dates" then?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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!!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post by Celauran »

Sure it is. You aren't working with dates, you're working with timestamps.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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;}
  }
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

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

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

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

Post 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
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

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

Post 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.
Post Reply