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

Day Of Week - showing date for invalid date??

Postby simonmlewis » Fri Apr 27, 2012 4:40 am

Syntax: [ Download ] [ Hide ]
$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?
simonmlewis
DevNet Resident
 
Posts: 2372
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom

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

Postby mikosiko » Fri Apr 27, 2012 8:19 am

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
mikosiko
Forum Regular
 
Posts: 750
Joined: Wed Jan 13, 2010 8:22 pm

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

Postby simonmlewis » Fri Apr 27, 2012 8:24 am

So it can be wrong?
simonmlewis
DevNet Resident
 
Posts: 2372
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom

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

Postby Celauran » Fri Apr 27, 2012 8:33 am

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.
User avatar
Celauran
Moderator
 
Posts: 3674
Joined: Tue Nov 09, 2010 3:39 pm
Location: Montreal, Canada

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

Postby simonmlewis » Fri Apr 27, 2012 8:36 am

In this case, incorrectly.
I'm telling it the date, and it's telling me the day - even tho the date does not exist.
simonmlewis
DevNet Resident
 
Posts: 2372
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom

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

Postby mikosiko » Fri Apr 27, 2012 8:42 am

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 ...
mikosiko
Forum Regular
 
Posts: 750
Joined: Wed Jan 13, 2010 8:22 pm

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

Postby simonmlewis » Fri Apr 27, 2012 8:48 am

So the way PHP/Apache works out the date, is not based on "real dates" then?
simonmlewis
DevNet Resident
 
Posts: 2372
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom

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

Postby simonmlewis » Fri Apr 27, 2012 8:53 am

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!!
simonmlewis
DevNet Resident
 
Posts: 2372
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom

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

Postby Celauran » Fri Apr 27, 2012 8:53 am

Sure it is. You aren't working with dates, you're working with timestamps.
User avatar
Celauran
Moderator
 
Posts: 3674
Joined: Tue Nov 09, 2010 3:39 pm
Location: Montreal, Canada

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

Postby simonmlewis » Fri Apr 27, 2012 8:57 am

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.

Syntax: [ Download ] [ Hide ]
  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;}
  }
simonmlewis
DevNet Resident
 
Posts: 2372
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom

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

Postby x_mutatis_mutandis_x » Fri Apr 27, 2012 12:08 pm

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.

Syntax: [ Download ] [ Hide ]
  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.
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??

Postby pickle » Fri Apr 27, 2012 12:20 pm

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

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

Postby x_mutatis_mutandis_x » Fri Apr 27, 2012 12:25 pm

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.
x_mutatis_mutandis_x
Forum Contributor
 
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm


Return to PHP - Code

Who is online

Users browsing this forum: Exabot [Bot] and 2 guests