PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Thu Nov 27, 2014 1:36 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 13 posts ] 
Author Message
PostPosted: Fri Apr 27, 2012 4:40 am 
Offline
DevNet Resident

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 2388
Location: United Kingdom
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?


Top
 Profile  
 
PostPosted: Fri Apr 27, 2012 8:19 am 
Offline
Forum Regular

Joined: Wed Jan 13, 2010 8:22 pm
Posts: 754
clearly documented in the manual
http://php.net/manual/en/function.mktime.php

Quote:
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


Top
 Profile  
 
PostPosted: Fri Apr 27, 2012 8:24 am 
Offline
DevNet Resident

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


Top
 Profile  
 
PostPosted: Fri Apr 27, 2012 8:33 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 3787
Location: Montreal, Canada
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.

_________________
Stay on top of upgrades.
Supported PHP versions
No longer supported versions


Top
 Profile  
 
PostPosted: Fri Apr 27, 2012 8:36 am 
Offline
DevNet Resident

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


Top
 Profile  
 
PostPosted: Fri Apr 27, 2012 8:42 am 
Offline
Forum Regular

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


Top
 Profile  
 
PostPosted: Fri Apr 27, 2012 8:48 am 
Offline
DevNet Resident

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


Top
 Profile  
 
PostPosted: Fri Apr 27, 2012 8:53 am 
Offline
DevNet Resident

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


Top
 Profile  
 
PostPosted: Fri Apr 27, 2012 8:53 am 
Offline
Moderator
User avatar

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

_________________
Stay on top of upgrades.
Supported PHP versions
No longer supported versions


Top
 Profile  
 
PostPosted: Fri Apr 27, 2012 8:57 am 
Offline
DevNet Resident

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 2388
Location: United Kingdom
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;}
  }


Top
 Profile  
 
PostPosted: Fri Apr 27, 2012 12:08 pm 
Offline
Forum Contributor

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


Top
 Profile  
 
PostPosted: Fri Apr 27, 2012 12:20 pm 
Offline
Briney Mod
User avatar

Joined: Mon Jan 19, 2004 7:11 pm
Posts: 6433
Location: 53.01N x 112.48W
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.


Top
 Profile  
 
PostPosted: Fri Apr 27, 2012 12:25 pm 
Offline
Forum Contributor

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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 15 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group