I want to implement Birthday countdown time. Normally, birthday normally takes place once in a year eg within 365 days. I downloaded this code from stackoverflow to check the next birthday day of someone who was born on august 22 1982. the days count down should be less than 365 days but the code below is giving a higher value with negative sign.
$rem = strtotime('1982-22-08') - time();
$day = floor($rem / 86400);
$hr = floor(($rem % 86400) / 3600);
$min = floor(($rem % 3600) / 60);
$sec = ($rem % 60);
if($day) echo "$day Days ";
if($hr) echo "$hr Hours ";
if($min) echo "$min Minutes ";
if($sec) echo "$sec Seconds ";
echo "Remaining...";
the next birthday countdown should be less than 365 days
Thanks
Its because you have wrong date format in strtotime and return nothing.
Documentation says
strtotime — Parse about any English textual datetime description into
a Unix timestamp
So if you use
strtotime('08/22/18')
it will work.
Also you should take actual year if you want say "hey your birthday in this is will be..."
What about trying this method:
$birth_month = 8;
$birth_day = 22;
$year = date('Y');
$month = date('n');
$day = date('j');
if ($birth_month < $month || ($birth_month == $month && $birth_day < $day))
$year++;
$target = mktime(0,0,0,$birth_month,$birth_day,$year);
$today = time();
$rem = abs($target - $today);
$days = (int)($rem/86400);
echo "Remaining days till next birthday: $days days!";
Related
I have this problem. Can someone help me,how to convert number of days into the format XX Years, XX Month, XX Days...
i created this function,
function convert($sum) {
$years = ($sum / 365) ;
$years = floor($years);
$month = ($sum % 365) / 30.5;
$month = floor($month);
$days = ($sum % 365) % 30.5; // the rest of days
// Echo all information set
echo 'DAYS RECEIVE : '.$sum.' days<br>';
echo $years.' years - '.$month.' month - '.$days.' days';
}
convert(151);
But with 151 days the result was wrong
DAYS RECEIVE : 151 days0 years - 4 month - 1 days
it must be 4 month ans 28 days not 1 day...
http://sandbox.onlinephpfunctions.com/code/f5e6b4b4f6a27024b66ffbf04e80698722a3ecab
If you use more modern PHP, the following is based around actual days in each month:
$days = 151;
$start_date = new DateTime();
$end_date = (new $start_date)->add(new DateInterval("P{$days}D") );
$dd = date_diff($start_date,$end_date);
echo $dd->y." years ".$dd->m." months ".$dd->d." days";
Note that it will vary, depending on the current date, so you might prefer to set $start_date and $end_date to work from a fixed baseline
$days = 151;
$start_date = new DateTime('1970-01-01');
$end_date = (new DateTime('1970-01-01'))->add(new DateInterval("P{$days}D") );
$dd = date_diff($start_date,$end_date);
echo $dd->y." years ".$dd->m." months ".$dd->d." days";
This is the solution for your problem:
function convert($sum) {
$years = floor($sum / 365);
$months = floor(($sum - ($years * 365))/30.5);
$days = ($sum - ($years * 365) - ($months * 30.5));
echo “Days received: ” . $sum . “ days <br />”;
echo $years . “ years, “ . $months . “months, “ . $days . “days”;
}
I need to get the age of a person. In case of an infant <= 7 days the output should be done in days. If the person is > 2 months and < 1 year the output should be months.
Here I got the problem, that some months are 31 other 30 or 28 days, so my solution isn't exact. Same problem for the else-case: Years with 366 days are ignored by my attempt, so the age isn't calculated correctly.
$timestamp = time();
$birthday_timestamp = mktime(0, 0, 0, $month, $day, $year);
$difference = $timestamp - $birthday_timestamp;
if ($difference < 1209600) return $output = ($difference / 86400)." days";
elseif ($difference < 5184000) return $output = ($difference / 86400 * 7). " weeks";
elseif ($difference < 31536000) return $output = ($difference / 86400 * 30). " months";
else return $output = ($difference / 86400 * 365). " years";
Don't try to calculate dates and differences between dates yourself. PHP has some very nice classes to do that for you.
$now = new DateTime();
$birthDay = new DateTime('1985-05-24');
$diff = $birthDay->diff($now);
var_dump($diff);
This is safe and takes into account leap years and other strange things that will occur when calculating with dates.
$diff will be a DateInterval and contains properties like $y, $m and $d.
I need to extract the total hours in a any month, given just the MONTH and the YEAR, taking into account leap years.
Here is my code so far...
$MonthName = "January";
$Year = "2013";
$TimestampofMonth = strtotime("$MonthName $Year");
$TotalMinutesinMonth = $TimestampofMonth / 60 // to convert to minutes
$TotalHoursinMonth = $TotalMinutesinMonth / 60 // to convert to hours
Just work out the number of days in the month and then multiply by 24, like so:
// Set the date in any format
$date = '01/01/2013';
// another possible format etc...
$date = 'January 1st, 2013';
// Get the number of days in the month
$days = date('t', strtotime($date));
// Write out the days
echo $days;
You can do this:
<?php
$MonthName = "January";
$Year = "2013";
$days = date("t", strtotime("$MonthName 1st, $Year"));
echo $days * 24;
You can use DateTime::createFromFormat since you don't have day
$date = DateTime::createFromFormat("F Y", "January 2013");
printf("%s hr(s)",$date->format("t") * 24);
Well if you are looking at working day its a different approach
$date = "January 2013"; // You only know Month and year
$workHours = 10; // 10hurs a day
$start = DateTime::createFromFormat("F Y d", "$date 1"); // added first
printf("%s hr(s)", $start->format("t") * 24);
// if you are only looking at working days
$end = clone $start;
$end->modify(sprintf("+%d day", $start->format("t") - 1));
$interval = new DateInterval("P1D"); // Interval
var_dump($start, $end);
$hr = 0;
foreach(new DatePeriod($start, $interval, $end) as $day) {
// Exclude sarturday & Sunday
if ($day->format('N') < 6) {
$hr += $workHours; // add working hours
}
}
printf("%s hr(s)", $hr);
<?php
function get_days_in_month($month, $year)
{
return $month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year %400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31);
}
$month = 4;
$year = 2013;
$total_hours = 24 * get_days_in_month($month, $year);
?>
you can use above function to retrieve total days in a month taking into account leap year and then multiply the value to 24
plus, you can also use a cal_days_in_month function but it only supports PHP builds of PHP 4.0.7 and higher.
and if you are using the above "get_day_in_month" then you need to parse the string into integer which can be done like this
FOR MONTH
<?php
$date = date_parse('July');
$month_int = $date['month'];
?>
FOR YEAR
<?php
$year_string = "2013"
$year_int = (int) $year_string
?>
Ok, here is my problem. Instead of measuring 7 days in seconds, I want to count how many weeks (Sunday Through Saturday) there are from date #1 to today.
PHP
$today1 = date("Y-m-d");
$diff = strtotime($date1,0) - strtotime($today1,0);
echo (floor($diff / 604800));
If you're counting in seconds, why use date() when you can use time() instead- it gives out the numeric time signature of your current time, making calculations such as this much easier.
Using seconds is fine, perhaps try something like this:
$date1 = "2012-12-25";
$today1 = time();
$diff = strtotime($date1) - $today1;
if($diff < 604800) {
$week = "this week";
} else {
$week = (floor($diff / 604800) == 1)
? floor($diff / 604800) . " week away" : floor($diff / 604800) . " weeks away";
}
echo $week;
Hay, i have a database holding events. There are 2 fields 'start' and 'end', these contain timestamps. When an admin enters these dates, they only have the ability to set the day,month,year. So we are only dealing with stamps containing days,months,years, not hours,minutes,seconds (hours,minutes and seconds are set to 0,0,0).
I have an event with the start time as 1262304000 and the end time as 1262908800. These convert to Jan 1 2010 and Jan 8 2010. How would i get all the days between these timestamps? I want to be able to return Jan 2 2010 (1262390400), Jan 3 2010 (1262476800) .. all the way to the end stamp. These events could cross over into different months, say May 28 to June 14.
Any ideas how to do this?
You just have to calculate the number of seconds between the two dates, then divide to get days :
$numDays = abs($smallestTimestamp - $biggestTimestamp)/60/60/24;
Then, you can use a for loop to retrieve the dates :
$numDays = abs($smallestTimestamp - $biggestTimestamp)/60/60/24;
for ($i = 1; $i < $numDays; $i++) {
echo date('Y m d', strtotime("+{$i} day", $smallestTimestamp)) . '<br />';
}
Again, if you don't know which timestamp is the smallest, you can use the min() function (second argument in strtotime).
I think that a quick workaround for this is to subtract the amount of a days worth of seconds from the end_stamp until you get to the start_tag.
//1 day = 86400 seconds
I would build an array of the days to use later.
EDIT (example)
$difference = 86400;
$days = array();
while ( $start_time < $end_time )
{
$days[] = date('M j Y', $end_time);
$end_time -= $difference;
}
This should cover any time frame even if its over a bunch of months.
Try this:
while($date_start <= $date_end) {
echo date('M d Y', $date_start) . '<br>';
$date_start = $date_start + 86400;
}
Hope this helps !
$d1=mktime(22,0,0,1,1,2007);
$d2=mktime(0,0,0,1,2,2007);
echo "Hours difference = ".floor(($d2-$d1)/3600) . "<br>";
echo "Minutes difference = ".floor(($d2-$d1)/60) . "<br>";
echo "Seconds difference = " .($d2-$d1). "<br>";
echo "Month difference = ".floor(($d2-$d1)/2628000) . "<br>";
echo "Days difference = ".floor(($d2-$d1)/86400) . "<br>";
echo "Year difference = ".floor(($d2-$d1)/31536000) . "<br>";
http://www.plus2net.com/php_tutorial/date-diff.php
http://www.phpf1.com/tutorial/php-date-difference.html
$daysInBetween = range($startTs, $endTs, 86400);
$secondDay = date('M d Y', $daysInBetween[1]);
/*
$thirdDay = date('M d Y', $daysInBetween[2]);
...
*/
Note that the range() function is inclusive.
**This is a very simple code for find days hours minutes and seconds in php**
$dbDate = strtotime("".$yourbdDate.""); // Database date
$endDate = time(); // current time
$diff = $endDate - $dbDate; /// diffrence
$days = floor($diff/86400); /// number of days
$hours = floor(($diff-$days*86400)/(60 * 60)); //// number of hours
$min = floor(($diff-($days*86400+$hours*3600))/60);///// numbers of minute
$second = $diff - ($days*86400+$hours*3600+$min*60); //// secondes
if($days > 0) echo $days." Days ago";
elseif($hours > 0) echo $hours." Hours ago";
elseif($min > 0) echo $min." Minute ago";
else echo "Just second ago";
Something like this?
$day = $start;
while ($day < $end) {
$day += 86400;
echo $day.' '.date('Y-m-d', $day).PHP_EOL;
}
By the way, 1262304000 is Dec 31, not Jan 1.
get the difference of two dates and divide it by 86400. abs(($date1 - $date2) / 86400) will produce the needed result