3

I want to develop BirthDay CountDown calculator in PHP

User input the date DD-MM-YYYY

I have a days differnce calculator, but it calculates the days from between two specified years.

$daysfrom = $_POST['daysfrom'];
$daysto = $_POST['daysto'];
echo 'Difference of days B.W <b>' . $daysfrom . '</b>-<b>'.$daysto.'</b> are';
    $daysto = strtotime($daysto);
    $daysfrom = strtotime($daysfrom);
    $datediff = $daysto - $daysfrom;
    echo floor($datediff / (60 * 60 * 24));

And I have tried this calculate days between birthday.(I know this is not right)

User will input MM-DD-YYYY and it should calculate days between months.

Ex: - Input: 13-03-1993 Output: (Assume todays date is 10-03-2014), it should display 03 Days left

 if (isset($_POST['daysto'])) {
       $daysto = $_POST['daysto'];
       echo 'Your birthday comes within ';
       $daysto = strtotime($daysto);
       $now = strtotime(date('d-m'));
       $datediff = $daysto - $now;
       echo $datediff / 60 / 60 / 24;
     }
  • @TrentonMaki read the answer properly it is not duplicate of given question – Satish Sharma May 24 '14 at 06:16
  • @user3671029 have you try my answer. – Satish Sharma May 24 '14 at 06:17
  • @SatishSharma What if user inputs only DD-MM. –  May 24 '14 at 06:46
  • put this as question i will answer it also. – Satish Sharma May 24 '14 at 06:54
  • you can use `$birthdate = "25-05".(date('Y')-1);` if inputis DD-MM – Satish Sharma May 24 '14 at 06:57
  • First off you're using the format incorrectly. The docs (http://www.php.net/manual/en/function.strtotime.php) say that, when dates are separated by -, they are assumed to be in d-m-y. Second, those same docs say that you should not use the basic arithmetic operators. You should use DateTime::add() and DateTime::sub() or DateTime::modify(). Finally, this is a duplicate. Check this answer: http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php – Mikayla Maki May 24 '14 at 09:07

2 Answers2

1

You could just change the birth date to have the same year as the current date, then run the date_diff().

$birthdate = strtotime("13-03-1993");
$today = strtotime("10-03-2014");
$fixedBirthdate = date_create(date("Y", $today) . "-" . date("m", $birthdate) . "-" . date("d", $birthdate));

$diff = date_diff(date_create(date("d-m-Y", $today)), $fixedBirthdate);
echo $diff->format("%R%a days");

There's probably a way to simplify some of that. I'm not that great with PHP syntax overall.

EDIT: With the use of date_parse_from_format(), I'd probably do something along these lines.

NOTE: This will work with both "13-03-1993" AND just "13-03".

$birthdateArray = date_parse_from_format("d-m-Y", "13-03");
$todayArray = date_parse_from_format("d-m-Y", "10-03-2014"); //In the real thing, this should instead grab the actual current date

$birthdate = date_create($todayArray["year"] . "-" . $birthdateArray["month"] . "-" . $birthdateArray["day"]);
$today = date_create("10-03-2014"); //This should also be actual current date

$diff = date_diff($today, $birthdate);
echo $diff->format("%R%a days");
BRW
  • 354
  • 3
  • 18
-1

you use this method.

$birthdate = "25-05-1993"; // desired input DD-MM-YYYY

//  $birthdate = "25-05-".(date('Y')-1);  if input DD-MM

$current_date = date("d-m-Y");  // current date 

$birth_time = strtotime($birthdate);
$current_time = strtotime($current_date);

$arr1 = explode("-", $birthdate);
$year1 = $arr1[2];

$arr2 = explode("-", $current_date);
$year2 = $arr2[2];

$year_diff = $year2-$year1;

$time_new = strtotime("+".$year_diff." year", $birth_time);

if($time_new<$current_time)
{
    $time_new = strtotime("+1 year", $time_new);
}

$time_diff = $time_new - $current_time;

$days = $time_diff/86400;

echo "Days Left:".$days;  // output

INPUT : 25-05-1993 OUTPUT : Days Left:1
INPUT : 20-04-1993 OUTPUT : Days Left:331

See DEMO

Satish Sharma
  • 9,547
  • 6
  • 29
  • 51