By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,701 Members | 2,021 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,701 IT Pros & Developers. It's quick & easy.

A "bad" Unix Timestamp?

P: n/a
I ran into an interesting gotcha with unix timestamps.

I've got a page where the user inputs the date with drop-down boxes
(easy to deal with), that my script sees as (say) $d $m $y, not
necessarily in that order, but you get the idea, nicely formatted with
leading zeroes and so on.

Then I run it through checkdate($m, $d, $y) just to make sure the user
doesn't think there's 31 days in September or whatnot.

That's all fine. Then I think, well, it would be nice if I could tell
the user something like "there's not that may days in $m" and I should
be able to use strtotime() and date() -- but it turns out (and rightly
so) that you can't create a "bad" Unix timestamp.

For instance,

date ("F d Y",strtotime("2004-09-31"));

will return October 01 2004 and fwiw, so will

date ("F d Y",mktime(0,0,0,9,31,2004));

I can write a switch, of course, and there's only 5 cases (30 days
hath September etc., plus February needs to be dealt with). It
intrigues me, though, and I wondered if anyone else had played with it.
--
Looks like more of Texas to me ...
-- from The Wild Bunch
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Wed, 20 Oct 2004, Warren Oates wrote:
it turns out (and rightly so) that you can't create a "bad" Unix
timestamp.

date ("F d Y",strtotime("2004-09-31"));

will return October 01 2004 and fwiw, so will

date ("F d Y",mktime(0,0,0,9,31,2004));


Rightly or wrongly, I personally have found this quite a useful "feature".
It means you can *always* calculate the last date in the current month:
mktime(0, 0, 0, date("m") + 1, 0, date("Y"))

or tomorrow:
mktime(0, 0, 0, date("m"), date("d") + 1, date("Y"))

This latter is nicer than time() + 86400 because that doesn't always
seem to return exactly midnight.

So in your example, you could determine if the user has entered 31 in a
month with only 30 days with:

if (date("d", mktime(0,0,0,$m+1,0,$y)) < $d) {
// not that many days in the selected month
}

Of course this assumes a valid $m and $y.

--
Matt
Jul 17 '05 #2

P: n/a
Warren Oates wrote:
<snip>
That's all fine. Then I think, well, it would be nice if I could tell
the user something like "there's not that may days in $m" and I should
be able to use strtotime() and date() -- but it turns out (and rightly
so) that you can't create a "bad" Unix timestamp.

For instance,

date ("F d Y",strtotime("2004-09-31"));

will return October 01 2004 and fwiw, so will

date ("F d Y",mktime(0,0,0,9,31,2004));

I can write a switch, of course, and there's only 5 cases (30 days
hath September etc., plus February needs to be dealt with). It
intrigues me, though, and I wondered if anyone else had played with it.


Something like this?

<?php
$user_date = '2004-09-31';
if (date('Y-m-d', strtotime($user_date)) != $user_date) {/* bad date */}
?>
--
USENET would be a better place if everybody read:
http://www.expita.com/nomime.html
http://www.netmeister.org/news/learn2quote2.html
http://www.catb.org/~esr/faqs/smart-questions.html
Jul 17 '05 #3

P: n/a
In article <sl*******************@ID-203069.user.uni-berlin.de>,
Pedro Graca <he****@hotpop.com> wrote:

:Something like this?
:
:<?php
:$user_date = '2004-09-31';
:if (date('Y-m-d', strtotime($user_date)) != $user_date) {/* bad date */}
:?>

which is sort of what I'm doing ...

[and also]

In article <Pi******************************@hermes.athens> ,
Matt Raines <matt@rai#remove#nes.me.uk> wrote:

:if (date("d", mktime(0,0,0,$m+1,0,$y)) < $d) {
: // not that many days in the selected month
:}
:
:Of course this assumes a valid $m and $y.

Which is cool. $m and $y are valid because the user can only choose
valid months and years. (I didn't create a month called Gazornanplat,
between August and September, although I've often wanted to.)

Thanks guys.
--
Looks like more of Texas to me ...
-- from The Wild Bunch
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.