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

simple php date question

P: n/a
Maybe I'm tired, but I'm having a small problem with a date function I'm
writing. Total noobish I know, but this is just going beyond me for some
reason...

What I want to happen is send a date (such as June 31, an invalid date) and
have the function turn it into a real date by subtraction. In other words,
the function would turn the date to June 30, run itself again, and if June
30 is a real date, it'd return it back to the program.

Seems simple enough, but I can't figure out why this isn't working. Here's
my code. If you have any suggestions, I'd be glad to hear them.

function real_date($month,$day,$year){
if(!$date=checkdate($month,$day,$year)){
$day=$day-1;
real_date($month,$day,$year);
} else {
echo 'made it to valid';
return date('m:d:Y',mktime(0,0,0,$month,$day,$year));
}
}
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
*** gsv2com wrote/escribió (Thu, 2 Jun 2005 17:52:51 +0900):
function real_date($month,$day,$year){
if(!$date=checkdate($month,$day,$year)){
$day=$day-1;
real_date($month,$day,$year);
} else {
echo 'made it to valid';
return date('m:d:Y',mktime(0,0,0,$month,$day,$year));
}
}


If $month is not between 1 and 12 or $year is not between 1 and 32767
you'll enter a infinite loop. I cannot really see the logic of your
algorithm. If user inputs 30 February I find it more sensible to turn it
into 2 March. That's what mktime() does by itself. Quoting from manual:

mktime() is useful for doing date arithmetic and validation, as it will
automatically calculate the correct value for out-of-range input. For
example, each of the following lines produces the string "Jan-01-1998".

<?php
echo date("M-d-Y", mktime(0, 0, 0, 12, 32, 1997));
echo date("M-d-Y", mktime(0, 0, 0, 13, 1, 1997));
echo date("M-d-Y", mktime(0, 0, 0, 1, 1, 1998));
echo date("M-d-Y", mktime(0, 0, 0, 1, 1, 98));
?>
--
-- Álvaro G. Vicario - Burgos, Spain
-- http://bits.demogracia.com - Mi sitio sobre programación web
-- Don't e-mail me your questions, post them to the group
--
Jul 17 '05 #2

P: n/a
NC
gsv2com wrote:

I'm having a small problem with a date function I'm writing.

What I want to happen is send a date (such as June 31, an invalid
date) and have the function turn it into a real date by subtraction.
Unfortunately, this is ambiguous. Let's say you send in a date
where $year=2005, $month=01, and $day=65. What do you want it
to be reduced to -- last day of January? last day of February?
Something else? By the way, if you feed this to PHP, let's say,
via date('Y-m-d', strtotime('2005-01-65')), you will obtain
a valid date, '2005-03-06', which is the 65th day since the
beginning of January 2005...
In other words, the function would turn the date to June 30, run
itself again, and if June 30 is a real date, it'd return it back
to the program.


OK, but how do you want the function to return the date? You are
submitting three arguments. What do you want back -- three numbers
(if so, how -- in an array or simply by modifying the arguments),
a string 'YYYY-MM-DD', a timestamp?

Also, I think there's no need to use recursive calls here; recursion
is expensive and should be used sparingly. A cycle will do just
fine...

Cheers,
NC

Jul 17 '05 #3

P: n/a
Why would a month NOT be between 1 and 12 and year NOT be between 1 and
32767? :P

The reason I was having to do it this way is because a company I am sending
a query to has a (buggy) program that falls to pieces, for instance, if i
send a date that hasn't yet happened IF it's a month ahead of the current
month. For instance, if I sent them a query with June 31 as the last date,
mktime would send them July 1 and I'd be faced with the white screen of
emptiness.

"Alvaro G Vicario" <al******************@telecomputeronline.com> wrote in
message news:ji******************************@40tude.net.. .
*** gsv2com wrote/escribió (Thu, 2 Jun 2005 17:52:51 +0900):
function real_date($month,$day,$year){
if(!$date=checkdate($month,$day,$year)){
$day=$day-1;
real_date($month,$day,$year);
} else {
echo 'made it to valid';
return date('m:d:Y',mktime(0,0,0,$month,$day,$year));
}
}


If $month is not between 1 and 12 or $year is not between 1 and 32767
you'll enter a infinite loop. I cannot really see the logic of your
algorithm. If user inputs 30 February I find it more sensible to turn it
into 2 March. That's what mktime() does by itself. Quoting from manual:

mktime() is useful for doing date arithmetic and validation, as it will
automatically calculate the correct value for out-of-range input. For
example, each of the following lines produces the string "Jan-01-1998".

<?php
echo date("M-d-Y", mktime(0, 0, 0, 12, 32, 1997));
echo date("M-d-Y", mktime(0, 0, 0, 13, 1, 1997));
echo date("M-d-Y", mktime(0, 0, 0, 1, 1, 1998));
echo date("M-d-Y", mktime(0, 0, 0, 1, 1, 98));
?>
--
-- Álvaro G. Vicario - Burgos, Spain
-- http://bits.demogracia.com - Mi sitio sobre programación web
-- Don't e-mail me your questions, post them to the group
--

Jul 17 '05 #4

P: n/a
Thanks NC. What I'm doing is returning a string like 06:30:2005 and then
pulling the month, date, and year from it after it has been returned. I'll
keep playing around with this.

dk

"NC" <nc@iname.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
gsv2com wrote:

I'm having a small problem with a date function I'm writing.

What I want to happen is send a date (such as June 31, an invalid
date) and have the function turn it into a real date by subtraction.


Unfortunately, this is ambiguous. Let's say you send in a date
where $year=2005, $month=01, and $day=65. What do you want it
to be reduced to -- last day of January? last day of February?
Something else? By the way, if you feed this to PHP, let's say,
via date('Y-m-d', strtotime('2005-01-65')), you will obtain
a valid date, '2005-03-06', which is the 65th day since the
beginning of January 2005...
In other words, the function would turn the date to June 30, run
itself again, and if June 30 is a real date, it'd return it back
to the program.


OK, but how do you want the function to return the date? You are
submitting three arguments. What do you want back -- three numbers
(if so, how -- in an array or simply by modifying the arguments),
a string 'YYYY-MM-DD', a timestamp?

Also, I think there's no need to use recursive calls here; recursion
is expensive and should be used sparingly. A cycle will do just
fine...

Cheers,
NC

Jul 17 '05 #5

P: n/a
This worked like a champ.

function real_date($month,$day,$year){
if(checkdate($month,$day,$year)){
return date('m:d:Y',mktime(0,0,0,$month,$day,$year));
} else {
return date('m:t:Y',mktime(0,0,0,$month,1,$year));
}
}

Thanks for your comments guys.
"gsv2com" <dk@sunny-net.ne.jp> wrote in message
news:d7*********@enews1.newsguy.com...
Maybe I'm tired, but I'm having a small problem with a date function I'm
writing. Total noobish I know, but this is just going beyond me for some
reason...

What I want to happen is send a date (such as June 31, an invalid date)
and have the function turn it into a real date by subtraction. In other
words, the function would turn the date to June 30, run itself again, and
if June 30 is a real date, it'd return it back to the program.

Seems simple enough, but I can't figure out why this isn't working. Here's
my code. If you have any suggestions, I'd be glad to hear them.

function real_date($month,$day,$year){
if(!$date=checkdate($month,$day,$year)){
$day=$day-1;
real_date($month,$day,$year);
} else {
echo 'made it to valid';
return date('m:d:Y',mktime(0,0,0,$month,$day,$year));
}
}

Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.