...

View Full Version : Default values to todays date



holty
09-10-2003, 02:47 PM
I have 3 select (combo) boxes...


One for day (for loop 1-31)
One for Month - Values Jan, Feb, Mar etc (3 digit)
One for year..


How could I default the values to be set to the current date? I've done this in ASP but never in PHP - any ideas?

raf
09-10-2003, 03:31 PM
Sure. All you need is Date()
http://be.php.net/date

Then , when inside you'r loops to build the optionlists, you have something like


for ($i=1; $i<=31;$i ++){
$option .= "<option value=\"" . $i . "\"" ;
if Date(d) == $i {
$option .= " selected=\"selected\"";
}
$option .= ">" . $i . "</option>" ;
}
echo $option ;

So the option with the same dayvalue will be selected.
For the month, it's Date(m), and for the year, Date(Y)

holty
09-12-2003, 03:24 PM
Raf,

Thanks for your reply...

Currently I have the select field displaying the info selected (the form and results are on one page)

So Initally before a user has selected the date I would like it to be the current date. If the user has already selected the date and viewed the results, I would like the selected date remain there (if you get me!)

Here is my code...

<?php
for($i = 1; $i < 32; $i++){
echo '<option value="' .$i. '"'
.((isset($_GET['Day']) && $_GET['Day'] == $i) ? ' selected="selected"' : '').
'>' .$i. '</option>';
}

How could I get it to initally put the default date (day in this example)

Thanks

raf
09-12-2003, 04:23 PM
Ha yes. Why not first get the "selected" date and store it in a variabel, and then have the loop where whe check agains it. Like



if (isset ($_GET['Day'])) { //so when the first first loads, this will be False
$select = $_GET['Day'] ;
}
else {
$select = Date(d) ;
}
for ($i=1; $i<=31;$i ++){
$option .= "<option value=\"" . $i . "\"" ;
if $select == $i {
$option .= " selected=\"selected\"";
}
$option .= ">" . $i . "</option>" ;
}
echo $option ;

holty
09-15-2003, 10:32 AM
Raf,

Thanks thats great... works like a treat too.

My next prob is with the month. Currently it is like (for each month):

<option value="Jan" <?php if($_GET['Month'] == "Jan"){ echo 'selected="selected"';} ?> />Jan</option>

How could I get it to default to the current month if a month has not been selected? Would we need an else clause in each of the statements?

Thanks for your help :thumbsup:

raf
09-15-2003, 11:21 AM
if (isset ($_GET['Month'])) { //so when the first first loads, this will be False
$select = $_GET['Month'] ;
}
else {
$select = Date(M) ; // will return the current month as a 3 letter abreviation
}


Then, when you build the optionlist, you check if the option == $select . Like

<option value="Jan" <?php if($select == "Jan"){ echo 'selected="selected"';} ?> />Jan</option>

mordred
09-15-2003, 12:14 PM
I'm just going to add a minor correction to your code, raf. The following is slightly incorrect usage of the date() function:



$select = Date(M) ;


M is not a defined constant. What happens is that PHP *assumes* you rather meant M in a literal way, as if it would be a string, and treats it like that. This is general behaviour and not limited to the date function. However, depending on the error level you set, this triggers an error notice.
The format string really needs to be a string, like



$select = Date('M');


This allows you to combine various date identifier to form a date string:



$select = date('d.m.Y'); // example


Nitpicking as usually. :)

raf
09-15-2003, 04:34 PM
Originally posted by mordred
Nitpicking as usually. :)

It's a valid remark ... and now I also learned something today :thumbsup:

holty
09-16-2003, 09:30 AM
Thats great! Got that working a treat!

Theres another twist to the tale though now! It been decided that there will be 2 sets of dates - a date from and a date to. The date to is the current date (which we've done) and the date from is the current date -7 (a week previous).

The day from will simply be:

if (isset ($_GET['DayFrom'])) { //so when it first loads, this will be False
$select = $_GET['DayFrom'];
}
else {
$select = Date('d')-7 ;

etc....

How could we do the month from if say the current date is the 3rd of October - how can we make the month show September?

Cheers for all your help - I'm sure this is the last request on dates!! :p

mordred
09-16-2003, 11:19 AM
There's a function called strtotime() which you can utilize with natural language. For your case, to display the name of the month one week ago, simply do this:



$month = date('F', strtotime('1 week ago'));
print $month;

holty
09-16-2003, 11:42 AM
mordred - I can see how that works - thanks!

but when i try:

<?php
if (isset ($_GET['Month'])) { //so when the first first loads, this will be False
$select = $_GET['Month'] ;
}
else {
$select = Date('F', strtotime('1 week ago')); // will return the current month as a 3 letter abreviation
}
?>
<option value="Jan" <?php if($select == "Jan"){ echo 'selected="selected"';} ?> />Jan</option>
.....


It returns Jan - is this because the strtotime doesn't return it back as a 3 letter abbreviation? Any clues how to get the value back as Jan, Feb etc?

mordred
09-16-2003, 12:24 PM
Yes indeed, the modifier is not the correct one, I thought you wanted the whole month name - in this case you'd need date('F'), but what you're looking for seems to be date('M').

You can always look up the right modifier for your needs in this table:
http://us2.php.net/manual/en/function.date.php

holty
09-16-2003, 12:27 PM
Cheers - silly me!!

I can't test it thou because I can't change the date on the server but I trust it works!!

Thanks every1 for you help!:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum