473,624 Members | 2,612 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

simple php date question

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($mont h,$day,$year){
if(!$date=check date($month,$da y,$year)){
$day=$day-1;
real_date($mont h,$day,$year);
} else {
echo 'made it to valid';
return date('m:d:Y',mk time(0,0,0,$mon th,$day,$year)) ;
}
}
Jul 17 '05 #1
5 1720
*** gsv2com wrote/escribió (Thu, 2 Jun 2005 17:52:51 +0900):
function real_date($mont h,$day,$year){
if(!$date=check date($month,$da y,$year)){
$day=$day-1;
real_date($mont h,$day,$year);
} else {
echo 'made it to valid';
return date('m:d:Y',mk time(0,0,0,$mon th,$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
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
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************ ******@telecomp uteronline.com> wrote in
message news:ji******** *************** *******@40tude. net...
*** gsv2com wrote/escribió (Thu, 2 Jun 2005 17:52:51 +0900):
function real_date($mont h,$day,$year){
if(!$date=check date($month,$da y,$year)){
$day=$day-1;
real_date($mont h,$day,$year);
} else {
echo 'made it to valid';
return date('m:d:Y',mk time(0,0,0,$mon th,$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
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.goo glegroups.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
This worked like a champ.

function real_date($mont h,$day,$year){
if(checkdate($m onth,$day,$year )){
return date('m:d:Y',mk time(0,0,0,$mon th,$day,$year)) ;
} else {
return date('m:t:Y',mk time(0,0,0,$mon th,1,$year));
}
}

Thanks for your comments guys.
"gsv2com" <dk@sunny-net.ne.jp> wrote in message
news:d7******** *@enews1.newsgu y.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($mont h,$day,$year){
if(!$date=check date($month,$da y,$year)){
$day=$day-1;
real_date($mont h,$day,$year);
} else {
echo 'made it to valid';
return date('m:d:Y',mk time(0,0,0,$mon th,$day,$year)) ;
}
}

Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1550
by: gene.ellis | last post by:
Hello everyone, I am really having a lot of trouble understanding the DOM and I doing something quite basic. I need to the DOM (in PHP 5) to modify an XML file. For example the XML basically looks like this. <news> <pageContent> <heading></heading> <author></author>
13
2351
by: LRW | last post by:
Having a problem getting a onSubmit function to work, to where it popsup a confirmation depending on which radiobutton is selected. Here's what I have: function checkdel() { if (document.getElementById"].value=='1') { confirm('Are you sure you want to delete this file?'); } } ......
3
2065
by: trinity | last post by:
I have two columns: Plantdate Totalpower 9/26/02 6217 10/31/02 5187 11/28/02 4246 12/26/02 4132 1/23/03 4313 2/21/03 4416 3/21/03 4355
6
3779
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am trying to read a flatfile. In COBOL, my simple file layout and READ statement would look like below. Question: what is the standard, simple coding convention for reading in a flatfile - one record at a time?? SCANF does not work because of...
2
8386
by: Hazzard | last post by:
I just realized that the code I inherited is using all asp.net server controls (ie. webform controls) and when I try to update textboxes on the client side, I lose the new value of the textbox when submitting the form to update the database. The server doesn't have the client side value any more. It seems to me that as I begin to write the client side javacript code for form validation and client side editing capabilities in order to save...
18
3026
by: Q. John Chen | last post by:
I have Vidation Controls First One: Simple exluce certain special characters: say no a or b or c in the string: * Second One: I required date be entered in "MM/DD/YYYY" format: //+4 How ??
7
6343
by: Scott Frankel | last post by:
Still too new to SQL to have run across this yet ... How does one return the latest row from a table, given multiple entries of varying data? i.e.: given a table that looks like this: color | date --------+------------ red | 2004-01-19 blue | 2004-05-24
1
1126
by: Phillip Vong | last post by:
I have a simple Formview and in the InsertMode I have a calendar and a textbox. All I want is for people to click on a date and the date is populated to the textbox. Here is my simple code and my problem is converting the DateTxtBox to a date format. Can someone show me how to do this simple conversion? Thanks! Protected Sub Calendar2_SelectionChanged(ByVal sender As Object, ByVal e As
1
1297
by: cppaddict | last post by:
I have dates like "8/27" or "9/4", and I need to convert them to date objects. When choosing a year, we should go backwards until we find the first match. That is, if the script runs on Jan 1, 2008 and encounters the string "8/27", it should return a date for "8/27/2007". I've been playing around with strtodate and some other functions but I can't figure this out, and as there must be a simple solution I'd like to avoid writing a...
0
8182
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8688
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8635
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8494
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6115
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2614
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1800
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.