Jump to content

Problem with birthday countdown script.


sumolotokai

Recommended Posts

Hello,

 

I am having issues creating a birthday countdown script. I have tried all sorts of things as you can see.

It seems that am only one step away from success but am missing something. 

 

If i have done something completely stupid, please just point me in the right direction.

 

Sorry for the mess...

 

$x = $_POST["name"];
if (strstr($x,"-2")) {
echo "There are $x days until your birthday";}
else {
echo "Your date of birth is not in the correct format, please press backwards and try again.";}
$y = explode("-", $x);
$mydob = mktime(0,0,0,$y[1],$y[0],$y[2]);
print $mydob;
echo date("d/m/y", $mydob);
$today = time();
settype($mydob, 'integer');
settype($today, 'integer');
echo "<br>";
print $mydob - $today;
$remain = $mydob - $today;
echo "<br>";
print $mydob;
echo "<br>";
print $today;
echo "<br>";
print date($remain);
echo "<br>";
print $remain;
echo "<br>";
echo "There are..." . date("m/d/y g.i:s",$remain)

 

tml>

<head>

<title>Birthdate Generator</Title>

</head>

<body>

<form action="birthday.php" method="post">

<h1>The number of days until your birthday!</h1>

<p><strong>When is your next birthday dd-mm-yyyy</strong><br> <input type="text" size="25" name="name"></p>

<p><input type="submit" value="send"></p>

</form>

</body>

</html>

Link to comment
Share on other sites

well your a little off. ignore the year in their birthday unless your calculating how old they are.

 

then you need to see if the date (day and month) is greater than the current date. if not you need to add one to the year because their birthday won't happen until next year.

 

then create the timestamp. then take the difference from the current date.

 

once you have the difference divide it by 86400 (number of seconds in a day. 60*60*24) and ceil it. This will give you the number of days until that date ie. birthday.

 

hope that helps.

 

Link to comment
Share on other sites

Here is an all-in-one page. Modify for your needs. Note: I did not include any validation of the user input.

 

<?php

$birthdate = explode('-', $_POST['name']);
$day   = $birthdate[0];
$month = $birthdate[1];
$year  = $birthdate[2];

$today = mktime();
$birthDateThisYear = mktime(0,0,0,$month,$day);
if ($birthDateThisYear<$today)
{
    $nextBirthDate = mktime(0,0,0,$month,$day,date('Y',$today+1));
}
else
{
    $nextBirthDate = $birthDateThisYear;
}

$daysTillNextBirthday = floor(($nextBirthDate-$today)/60/60/24);

?>
tml>
<head>
<title>Birthdate Generator</Title>
</head>
<body>
<?php echo "Days till next birthday: {$daysTillNextBirthday}"; ?>
<form action="birthday.php" method="post">
<h1>The number of days until your birthday!</h1>
<p><strong>When is your next birthday dd-mm-yyyy</strong><br> <input type="text" size="25" name="name"></p>
<p><input type="submit" value="send"></p>
</form>
</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.