Jump to content

year, date, day to convert to total hour how to? please help

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
yuki

yuki

    Newbie

  • Members
  • Pip
  • 4 posts
I need to convert of below program's output(year, date, day) into total hour from base day, month year, can you please help?
<?

// This is a simple script to calculate the difference between two dates
// and express it in years, months and days
// 
// use as in: "my daughter is 4 years, 2 month and 17 days old" ... :-)
//
// Feel free to use this script for whatever you want
// 
// version 0.1 / 2002-10-3
//
// please send comments and feedback to [email]webmaster@lotekk.net[/email]
//

// ****************************************************************************

// configure the base date here
$base_day		= 11;		// no leading "0"
$base_mon		= 5;		// no leading "0"
$base_yr		= 2001;		// use 4 digit years!

// get the current date (today) -- change this if you need a fixed date
$current_day		= date ("j");
$current_mon		= date ("n");
$current_yr		= date ("Y");

// and now .... calculate the difference! :-)

// overflow is always caused by max days of $base_mon
// so we need to know how many days $base_mon had
$base_mon_max		= date ("t",mktime (0,0,0,$base_mon,$base_day,$base_yr));

// days left till the end of that month
$base_day_diff 		= $base_mon_max - $base_day;

// month left till end of that year
// substract one to handle overflow correctly
$base_mon_diff 		= 12 - $base_mon - 1;

// start on jan 1st of the next year
$start_day		= 1;
$start_mon		= 1;
$start_yr		= $base_yr + 1;

// difference to that 1st of jan
$day_diff	= ($current_day - $start_day) + 1; 	// add today
$mon_diff	= ($current_mon - $start_mon) + 1;	// add current month
$yr_diff	= ($current_yr - $start_yr);

// and add the rest of $base_yr
$day_diff	= $day_diff + $base_day_diff;
$mon_diff	= $mon_diff + $base_mon_diff;

// handle overflow of days
if ($day_diff >= $base_mon_max)
{
	$day_diff = $day_diff - $base_mon_max;
	$mon_diff = $mon_diff + 1;
}

// handle overflow of years
if ($mon_diff >= 12)
{
	$mon_diff = $mon_diff - 12;
	$yr_diff = $yr_diff + 1;
}

// the results are here:

// $yr_diff  	--> the years between the two dates
// $mon_diff 	--> the month between the two dates
// $day_diff 	--> the days between the two dates

// ****************************************************************************

// simple output of the results 
print "The difference between <b>".$base_yr."-".$base_mon."-".$base_day."</b> ";
print "and <b>".$current_yr."-".$current_mon."-".$current_day."</b> is:";
print "<br><br>";

// this is just to make it look nicer
$years = "years";
$days = "days";
if ($yr_diff == "1") $years = "year";
if ($day_diff == "1") $days = "day";

// here we go
print $yr_diff." ".$years.", ";
print $mon_diff." month and ";
print $day_diff." ".$days;

?>

Edited by Orjan, 11 April 2010 - 06:43 AM.
Please use Code or php tags when posting


#2
SoN9ne

SoN9ne

    Programmer

  • Members
  • PipPipPipPip
  • 129 posts
Please use code tags so this will be easier to read :cool:

This is one way to do this. I am sure there are better methods but I haven't had to deal with comparisons like that
// 1 yr = 31556926 seconds
// 1 month = 2629743.83 secoonds
// 1 day = 86400 seconds

$year = $yr_diff * 31556926;
$month = $mon_diff * 2629743.83;
$day = $day_diff * 86400;
$totalSeconds = $year + $month + $day;
$totalHours = $totalSeconds / 60;

print "<br />{$totalHours} total hours";

FYI: I used Google to get the second counts. The year and months seemed odd to me then I remembered that a day is longer then 24 hours and then I started to wonder about the day in seconds.... The point is, this is an estimate not actual.
"Life would be so much easier if we only had the source code."

#3
yuki

yuki

    Newbie

  • Members
  • Pip
  • 4 posts
Thanks for your time and reply but I need the accurate measurement. Could some other persons give me hints?
----------------------------------------------
Giving unusual result in below code (after copy pesting this code end of program into my code):
The difference between 2010-4-9 and 2010-4-10 is:

0 years, 0 month and 1 day
1440 total hours
----------------------------------------------
1440 total hours???? in one day difference???

It can be 24 hours...am i right???

Thank you again.

SoN9ne said:

Please use code tags so this will be easier to read :cool:

This is one way to do this. I am sure there are better methods but I haven't had to deal with comparisons like that
// 1 yr = 31556926 seconds

// 1 month = 2629743.83 secoonds

// 1 day = 86400 seconds


$year = $yr_diff * 31556926;

$month = $mon_diff * 2629743.83;

$day = $day_diff * 86400;

$totalSeconds = $year + $month + $day;

$totalHours = $totalSeconds / 60;


print "<br />{$totalHours} total hours";

FYI: I used Google to get the second counts. The year and months seemed odd to me then I remembered that a day is longer then 24 hours and then I started to wonder about the day in seconds.... The point is, this is an estimate not actual.


#4
yuki

yuki

    Newbie

  • Members
  • Pip
  • 4 posts
Can anybody give me tips since I am very new to programming?

#5
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
the problem is that there was a simple mistake. he converted seconds to minutes and called it hours. yuo need to divide by 60 another time to get hours.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#6
SoN9ne

SoN9ne

    Programmer

  • Members
  • PipPipPipPip
  • 129 posts

Orjan said:

the problem is that there was a simple mistake. he converted seconds to minutes and called it hours. yuo need to divide by 60 another time to get hours.

:crying:

lol, nice catch.

This will give you hours instead of seconds....
$totalHours = ($totalSeconds / 60) / 60; 

"Life would be so much easier if we only had the source code."