Date Calculation Total Days - php

I have this code which is working fine for some dates which I don't understand why, below code should calculate the total days. If I select leavefrom = 2014-04-21 and leaveto = 2014-05-02, it gives me total of 8 days but it should be 9 nine days.
Here is the calendar:-
function total_day($leavefrom, $leaveto){
$start_date=strtotime($leavefrom);
$cur_day=$start_date;
$end_day=strtotime($leaveto);
$count=0;
$holiday=array("2014-05-01"=>"Labour Day", "2014-08-31"=>"Independence Day", "2014-12-25"=>"Christmas");
while(1){
//echo date("Y/m/d", $cur_day)."<br/>";
$cur_day=$cur_day +(3600*24);
//echo $count."S--".date("Y-m-d", $cur_day)."<-----E--".$end_day;
$day_of_week=date('w', $cur_day);
//echo "day_of_week-----".$day_of_week."<br/>";
if ($day_of_week == 0 || $day_of_week == 6) {
//No Operation
}else if(array_key_exists(date("Y-m-d", $cur_day), $holiday)){
//echo "Holiday because of ".$holiday[date("Y-m-d", $cur_day)];
}else{
$count++;
}
//echo "Total day--".$count."<br/><br/>";
if(($cur_day==($end_day+(3600*24)))||($cur_day>$end_day)){
break;
}
}
//$count = $count + 1;
return $count;
}
$totaldays = total_day($leavefrom, $leaveto);

I see no error there. From 2014-04-21 to 2014-05-02 there are 11 days out of which 2 weekends and 1 holiday (2014-05-01). Thus making 11 days - 3 days = 8 days
But If you want to include the starting date as well. Then you can use this:
while(1){
if($cur_day>$end_day){
break;
}
$day_of_week=date('l', $cur_day);
if (in_array($day_of_week,array('Saturday','Sunday'))) {
// NO operation
}else if(array_key_exists(date("Y-m-d", $cur_day), $holiday)){
// NO operation
}else{
$count++;
}
$cur_day=$cur_day +(3600*24);
}

Related

Object of class DateInterval could not be converted to int

Write a program that will input your name and birthday and will display the Date
and time of your input and display also your Age.
Also the system will greet you Good Morning / Afternoon/Evening.
The system will also inform you that you are young, very young, old, or very old
condition:
if age < 15 very young
age > 15 and < 20 young
age >= 20 and < 50 old
age >= 50 very old
im stuck on the condition of the machine determining whether i'm very young, young and old
if i run this code i always get the error of DateInterval could not be converted to int i'm at a loss of what to do
if (isset($_GET['submit'])) {
$result1= $_GET['Name'];
$result2= $_GET['Bday'];
$birthDate = $result2;
$currentDate = date("d-m-Y");
$age = date_diff(date_create($birthDate), date_create($currentDate));
date_default_timezone_set("Asia/Kolkata");
$h = date('G');
if($h>=5 && $h<=11)
{
echo "Good Morning $result1 ... <br>You are ".$age->format("%y years old, %m months and");
}
else if($h>=12 && $h<=15)
{
echo "Good Afternoon $result1 ... <br>You are ".$age->format("%y years old, %m months");
}
else
{
echo "Good Evening $result1... <br>You are ".$age->format("%y years old, %m months and");
}
if ($age < 15 )
{
echo "you are very young";
}
else if($age < 50)
{
echo "you are young";
}
else if($age < 51 )
{
echo "you are old";
}
else if ($age >= 50 )
{
echo "you are very old";
}

How to show month ago in PHP

$logintime value 1 year finished means, it will showing 1 years ago, but suppose 2 months only finished means I want to show 2 months ago, but my code showing like 60 days ago, I don't know where I did mistake, remaining hour, minutes this are working fine, only month making problem, $logintime = 2016-02-27 03:00:00
function timeAgo($logintime) {
date_default_timezone_set('UTC');
date_default_timezone_set('Asia/Kolkata');
$start_date = new DateTime($logintime);
$since_start = $start_date->diff(new DateTime(date("Y-m-d h:i:s")));
if (intval($since_start->format('%Y') ) >= 1) {
echo $year = $since_start->format('%Y years ago');
} else if (intval($since_start->format('%m')) >= 12) {
echo $months = $since_start->format('%m month ago');
} else if (intval($since_start->format('%a')) >= 1) {
echo $days = $since_start->format('%a days ago');
} else if (intval($since_start->format('%h')) >= 1) {
echo $hourss = $since_start->format('%h hours ago');
} else if (intval($since_start->format('%i')) >= 1) {
echo $min = $since_start->format('%i minuts ago');
} else if (intval($since_start->format('%s')) >= 1) {
echo $min = $since_start->format('%s seconds ago');
}
}
Your this line of code :
else if(intval($since_start->format('%m')) >= 12){
It says if the month > = 12, then show months ago, but you just have 2 months.
So you should consider changing it to :
else if(intval($since_start->format('%m')) >= 1){

Getting time left

I have a time left function that I am using to get the time left based on a sent parameter. My issue is I am having difficulty calculating if there is a day, a year, or a month left.
Function:
function get_time_difference_php_left($created_time)
{
$str = strtotime($created_time);
$today = strtotime(date('Y-m-d H:i:s'));
// It returns the time difference in Seconds...
$time_differnce = $today-$str;
// To Calculate the time difference in Years...
$years = 60*60*24*365;
// To Calculate the time difference in Months...
$months = 60*60*24*30;
// To Calculate the time difference in Days...
$days = 60*60*24;
// To Calculate the time difference in Hours...
$hours = 60*60;
// To Calculate the time difference in Minutes...
$minutes = 60;
if(intval($time_differnce/$years) > 1)
{
return " - ". intval($time_differnce/$years)." years left";
}else if(intval($time_differnce/$years) > 0)
{
return " - ".intval($time_differnce/$years)." year left";
}else if(intval($time_differnce/$months) > 1)
{
return " - ".intval($time_differnce/$months)." months left";
}else if(intval(($time_differnce/$months)) > 0)
{
return " - ".intval(($time_differnce/$months))." month left";
}else if(intval(($time_differnce/$days)) > 1)
{
return " - ".intval(($time_differnce/$days))." days left";
}else if (intval(($time_differnce/$days)) > 0)
{
return " - ".intval(($time_differnce/$days))." day left";
}else if (intval(($time_differnce/$hours)) > 1)
{
return " - ".intval(($time_differnce/$hours))." hours left";
}else if (intval(($time_differnce/$hours)) > 0)
{
return " - ".intval(($time_differnce/$hours))." hour left";
}else if (intval(($time_differnce/$minutes)) > 1)
{
return " - ".intval(($time_differnce/$minutes))." minutes left";
}else if (intval(($time_differnce/$minutes)) > 0)
{
return " - ".intval(($time_differnce/$minutes))." minute left";
}else if (intval(($time_differnce)) > 1)
{
return " - ".intval(($time_differnce))." seconds left";
}else
{
return " - few seconds left";
}
}
If I run this based on the now time and a date time of: 2014-04-17 03:27:26 it will tell me 88 years.
Suggestions, thoughts?
You had March initially. You are doing the calculation backwards then. Your code should show expiration date, not creation date. If you are trying to use an expiration date, then your code should use:
$time_differnce = $str - $today;
You can use the modulus (remainder) operator in each if to show the next value as well. For example if you have 3.7 days, you use 3 days and then .7*$hours.
Change all else if statements to if's
Change from:
if(intval($time_differnce/$years) > 1) {
return " - ". intval($time_differnce/$years)." years left";
} else if (intval($time_differnce/$years) > 0) {
to
if(intval($time_differnce/$years) > 1) {
return " - ". intval($time_differnce/$years)." years left";
}
if (intval($time_differnce/$years) > 0) {

Conditional on time difference to show a specific message only when the row didn't started yet

These are the inputs an database rows
$current 2012-07-26 15:30:00
1st $row['start'] 2012-07-26 14:00:00
2nd $row['start'] 2012-07-26 17:00:00
When I run the following code with the above current time, I get correctly the "Starts soon" for the 2nd row, but also I get it mistakenly on the 2nd row that it already started.
How do I edit this code to return me the "Starts soon" message only to the rows that will start in the next two hours?
$diff = strtotime($row['start']) - strtotime($current);
if ($diff < 7200) {
echo 'Starts soon';
} else if ($diff <= 0) {
echo 'Started';
} else {
echo 'Starts';
}
All $diff matching your second if clause (< 0) are already caught by the first if clause (< 7200) and never reach the second if in that else clause.
As a solution, restructure your code in the following way:
$diff = strtotime($row['start']) - strtotime($current);
if ($diff <= 0) {
echo 'Started';
} else if ($diff < 7200) {
echo 'Starts soon';
} else {
echo 'Starts';
}
EDIT
With respect to the question in comments:
If you mean the calendar day, you could use the following code:
if ( date( 'zY', $current ) == date( 'zY', $row['start'] ) {
// same day
} else {
// different days
}
If you mean just that both times are no more than 24 hours apart, use
if ( abs($current - $row['start']) < 24 * 60 * 60 ) {
// same day
} else {
// different day
}

How to calculate person's age in months+days in PHP 5.2?

I have asked this question before and accepted the answer but now I found that the php version on our server is 5.2 and DateTime::diff() is not working on that.
I want to calculate person's age in months plus days using date of birth and a given date.
Date Format Input: Y-m-d (example: 1986-08-23)
Output:
5 months and 20 days old.
150 months and 4 days old.
285 months and 30 days old.
Thanks
Here's a solution that'll accurately determine the number of months and number of days, including leap years. It assumes that things like July 21 to August 21 is 1 month 0 days, not 1 month 1 day, and that March 21 to April 20 is 0 months 30 days, not 1 month 0 days. The latter in both cases is what occurs when you just do a straight divide by 30 to calculate months.
I'm sure there's a better way to optimize the function, but it gets the job done:
function diff_date($start_date, $end_date) {
list($start_year, $start_month, $start_day) = explode('-', $start_date);
list($end_year, $end_month, $end_day) = explode('-', $end_date);
$month_diff = $end_month - $start_month;
$day_diff = $end_day - $start_day;
$months = $month_diff + ($end_year - $start_year) * 12;
$days = 0;
if ($day_diff > 0) {
$days = $day_diff;
}
else if ($day_diff < 0) {
$days = $end_day;
$months--;
if ($month_diff > 0) {
$days += 30 - $start_day;
if (in_array($start_month, array(1, 3, 5, 7, 8, 10, 12))) {
$days++;
}
else if ($start_month == 2) {
if (($start_year % 4 == 0 && $start_year % 100 != 0) || $start_year % 400 == 0) {
$days--;
}
else {
$days -= 2;
}
}
if (in_array($end_month - 1, array(1, 3, 5, 7, 8, 10, 12))) {
$days++;
}
else if ($end_month - 1 == 2) {
if (($end_year % 4 == 0 && $end_year % 100 != 0) || $end_year % 400 == 0) {
$days--;
}
else {
$days -= 2;
}
}
}
}
return array($months, $days);
}
list($months, $days) = diff_date('1984-05-26', '2010-04-29');
print $months . ' months and ' . $days . ' days old.';
Output:
314 months and 3 days old.
Edit: I tried to get rid of redundancy in the code, and forgot to rename a variable. This function will now work correctly for diff_date('2010-06-29', '2011-07-01').
Edit: Now correctly works for end months occurring after months with 31 or 28/29 days.
Use your favorite date parsing function (strtotime, strptime, mktime) to get an UNIX timestamp out of the date, then the interval ($now - $then)... and then work out how many seconds there are in a month and use that to calculate how many months the person has lived (division and remainder are your friends).
This'll give you a mathematically precise value that should be close enough to real life too.

Categories