Support Forums

Full Version: Find day of the week with PHP
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This will tell you the day of the week of any date you put in. I've done it to tell you the day you were born on but that's just what I initially made it for. This is something I did ages ago when I started learning PHP but this version is like a tenth the size of the original version, now that i understand mktime()

PHP Code:
<html>
<
head>
<
title>Birthday Calculator</title>
<
style type="text/css"
.
text {
color#000000;
font-family"Trebuchet MS";
}
</
style>
</
head>
<
body>
<
form method="get" action="">
    <
span class="text">Date:</span>
    <
select name="date" class="text">
        
        <
option value=""></option>
    <?
php
    
for($i=1;$i<=31;$i++)
    {
        
?>
        
        <option value="<?php echo $i?>"<?php if($_GET['date'] == $i) { echo " selected=\"selected\""; } ?>><?php echo $i?></option>
        <?php
    
}
    
?>
    
    </select>
    <?php
    $months 
= array("1" => "January",
                    
"2" => "February",
                    
"3" => "March",
                    
"4" => "April",
                    
"5" => "May",
                    
"6" => "June",
                    
"7" => "July",
                    
"8" => "August",
                    
"9" => "September",
                    
"10" => "October",
                    
"11" => "November",
                    
"12" => "December"
                    
);
    
?>
<span class="text">Month:</span>
    <select name="month" class="text">
        
        <option value=""></option>
        <?php
        
foreach($months as $mid => $month)
        {
            
?>
        
        <option value="<?php echo $mid?>"<?php if($_GET['month'] == $mid) { echo " selected=\"selected\""; } ?>><?php echo $month?></option>
            <?php
        
}
        
?>
        
    </select>
    <span class="text">Year:</span>
    <select name="year" class="text">
        
        <option value=""></option>
    <?php
    
for($i=2009;$i>=1900;$i--)
    {
        
?>
        
        <option value="<?php echo $i?>"<?php if($_GET['year'] == $i) { echo " selected=\"selected\""; } ?>><?php echo $i?></option>
        <?php
    
}
    
?>
    
    </select>
    <input type="submit" class="text" value="Submit" />
</form>
<?php
if($_GET)
{
    foreach(
$_GET as $key => $val)
    {
        
$_GET['$key'] = htmlspecialchars($val);
    }
    
$day_of_week date("l"mktime(000$_GET['month'] , $_GET['date'] , $_GET['year']));
    echo 
"<span class=\"text\">You were born on a " $day_of_week "</span>";
}
?>
</body>
</html> 

Put in your DoB and it'll say what day it was.
Thanks, this is cool.
That is nice. Will use this on my homepage, thanks.
haha funny little add-on!

niice!
wow its cool
Thanks Mattr

nice sharing
Nice tidy code MattR.
Expected nothing less but i do have a question regarding this.
What did you use to write this. What i mean is did you hand code it all in something like notepad etc?
Or from within a editor like Dreamweaver.
Main reason i ask is the indenting which i see. Very typical of dreamweaver to do it and certainly concidered standard.
Just curious is all to be honest.
I use Notepad++, I prefer to write it myself and just have syntax highlighting so I can see if something isn't right. The indenting is mainly just for readability/structure.
Cool thanks MattR. Very tidy work mate.