I have a number of X days, let's say 700.
How can I output this 700 to years?
Eg. 700 = 1.11 years (1 year 11 months)
or 2.2 (2 years and two months)
... And so on
I don't care for leap years or such, just need an aproximate value.
Think about it...
700 days, 30 days in a month.
700/30 = 23.33 months. 12 months in a year.
23.33/12 = 1.944444... take the integer value (int)1.944444 to get 1 for the year.
23.33%12= 11.33... months. You can cast it to an int as well to get 11
Your result: 1 year, 11 months
try this one...
//86400 seconds per day
//31556926 seconds per year
$days = 700;
$timestamp = (86400 * $days) / 31556926;
echo $timestamp;
// = 1.9165364839402 years
use this site for referrence: http://www.epochconverter.com/ they have conversion per month, week, days.
//1.9165364839402
Related
I'm having problem when getting the exact number of days. Given I have date/time which consider hours in counting number of days below the code give me zero days
$fisrstDate = new DateTime("2018-03-07 04:46:00");
$secondDate = new DateTime("2018-03-07 11:10:00");
$days=$fisrstDate->diff($secondDate)->days;
another example is this it should give me 2 days but shows only 1 days my idea is when 24 hours exceed I want to add another 1 days so that it would give me an output of 2 days
$fisrstDate = new DateTime("2018-03-07 04:46:00");
$secondDate = new DateTime("2018-03-08 05:00:00");
$days=$fisrstDate->diff($secondDate)->days;
You can use strtotime to get the exact seconds between two time stamps and then convert it to days followed by ceil to make it work. Eg:
$fisrstDate = strtotime("2018-03-07 04:46:00");
$secondDate = strtotime("2018-03-07 11:10:00");
$days = abs(ceil((abs($fisrstDate - $secondDate)/ (60 * 60 * 24)) - (1 / 24)));
echo $days;
Isn't it just date2 - date1 + 1?
This question already has answers here:
Calculate the number of months between two dates in PHP?
(16 answers)
Closed 6 years ago.
I would like to calculate number of calendar months between two days. For example:
2017-11-01 => 2017-11-30 = 1 (the same month)
2017-11-01 => 2017-12-01 = 2 (two different calendar months in the date)
2017-11-30 => 2017-12-01 = 2 (the same as above)
2017-11-15 => 2018-06-01 = 8 (6 + 2 different calendar months in the date)
How can I do that? So far I have:
$monthStart = new DateTime('2017-11-01');
$monthEnd = new DateTime('2017-12-01');
$dateDiff = $monthStart->diff($monthEnd);
$maxMonths = ($dateDiff->m + ($dateDiff->y*12) + $dateDiff->d>0?1:0);
But it doesn't work for some dates.
This QUESTION IS DIFFERENT from: Calculate the number of months between two dates in PHP? as I want to calculate the month every time the number for month changes. Not only when there is a 30 days difference between two days. Check the examples I have provided.
$a = "2007-01-01";
$b = "2008-05-31";
$i = date("Ym", strtotime($a));
while($i <= date("Ym", strtotime($b))){
echo $i."<br>";
if(substr($i, 4, 2) == "12")
$i = (date("Y", strtotime($i."01")) + 1)."01";
else
$i++;
}
output:
200701
200702
200703
200704
200705
200706
200707
200708
200709
200710
200711
200712
200801
200802
200803
200804
200805
I am trying to calculate no of hours of user. If many schedules assigned to him.
Schedules are -
10:00 AM - 12 pm
11 am - 12 pm
9 am - 12 pm
8 am - 5 pm
These schedules are assigned to a user.
I have to calculate no of hours how much time he is spending on these schedules.
Is there any shortest method to calculate this ?
I want answer = 8 hours. I dnt want to calculate the diff I want no of hours.
NO I want the result 8.. I explain why
10:00 AM - 12 pm --- 2 hour,
11 am - 12 pm ---- 1 hour already added ,
9 am - 12 pm ----- 3 hour but 10 - 12 already added so 1 hour,
8 am - 5 pm ---- 8 hours
Use this:
$minutes_diff = round(abs(strtotime($time1) - strtotime($time2)) / 60);
Or this:
$time1="7.30 AM";
$time2="8.30 PM";
$d1= strtotime($time1);
$d2= strtotime($time2);
$diff=$d2-$d1;
//Print the difference in hours : minutes
echo date("H:i",$diff);
I wrote this script to sum the total amount of hours in a work schedule from start of the work day until the end:
// $key['date'] contains a date format of 'yyyy-mm-dd'
// $key['start'] and $key['end'] contains a time of format hh:mm:ss
$start = new DateTime( $key['date'] .'T' . $key['start'] );
$end = new DateTime( $key['date'] .'T' . $key['end'] );
// difference returns the differential between start and end resulting in number of hours worked
$difference = $start->diff( $end );
// Additionally, I would like 1 hour and 15 minutes to result in 1.25 instead of 1.15 and so on. For this I used this:
$minute = $difference->format( '%i' )*1.666666666667;
$total = $difference->format( '%h' ) . '.' . $minute;
I'm going to write a function to print a number of days left between two dates. I would like it to tell the months and days left. For example:
45 days = 1month, 15 days
65 days = 2months, 5 days
10 days = 10 days
So I tried:
<?
$days=50;
if($days>"31"){
$time=$days/30;
}
echo $time;//1.67 month
?>
According to the code above. I expect the result to be like:
1 month, 20 days
Could you guys please suggest.
Try:
$days = 50;
if ($days > 31){
$month = floor($days/30); // return lowest whole integer
$days = $days % 30; // calculate left days
}
echo $month . " => " . $days; // output `1 => 20`
Get the month number for both months and subtract. Add in calculation for year change
Get day of months for both dates. If date2 > date1, subtract and you have the number of days, else remove 1 from month count and sumteact date
Basically, I am trying to recreate PHP date's year functionality. Using the number of seconds since 1 January 1970, I am trying to get the year with out using a built in function. I had a an idea, but it did not work because of leap years. Can anyone give me a working formula that takes the seconds since 1970 and gets a year from it?
To find the year you need to deal with leaps.
The years from 1 are ordered as blocks of 4 years been the last of them one day longer, right? So you have blocks of:
seconds_block = 365*3 + 366 days = 126230400 seconds
seconds_year = 365 days = 31536000 seconds
1970 is the second year of its block so with this:
<?php
//test_year.php
$given_seconds = $argv[1];
$seconds_year = 31536000;
$seconds_block = 126230400;
$total_blocks_to_1968 = 492;
$actual_block = floor((($given_seconds + $seconds_year) / $seconds_block)) + $total_blocks_to_1968;
$actual_offset_from_last_block = ($given_seconds + $seconds_year) % $seconds_block;
$actual_year_of_the_block = min(floor($actual_offset_from_last_block / $seconds_year) + 1, 4);
$actual_year = $actual_block * 4 + $actual_year_of_the_block;
echo $actual_year;
Testing it...
$ php test_year.php 0
1970
$ php test_year.php 1
1970
$ php test_year.php -1
1969
$ php test_year.php 31536000
1971
$ php test_year.php 31535999
1970
$ php test_year.php 126230400
1974
$ php test_year.php 126230399
1973
More:
One year is leap if is divisible by 4 except those divisible by 100 (but not by 400).
function isLeap(year){
return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)
}
EDIT: pseudocode formula
x = input // number of seconds since 1970
sy = 31536000 // seconds a non leap year
sb = 126230400 // seconds a block of 3 non leap years and one that is
actual_year = (floor(((x + sy) / sb)) + 492) * 4 +
(min(floor(((x + sy) % sb) / sy) + 1, 4));
Cheers
You can't ignore leap years.
You have year = 1970
Add numSecsInYear to that, increment the year
if year is leap, then 'eat' one more sec.
... until there is less that 1 year of seconds
then go month by month, day by day, hour by hour, min by min
also, year is leap if
- if ends with 00, if it gives no rest after dividing by 400
- else if it gives no rest after dividing by 4
You figure out the code :)
And yes, this isn't very much optimized :)
p.s. obviously, when going month by month, keep in mind that february has 29 days if year is leap :)
This is actually wrong, but a good enough approximation if you don't need the year to change exactly at the time of the new year. The idea is, the number of days in a year, in order for there to be leap years every 4 years, is 365.25 days.
$a = time();
$y = 1970 + floor($a / 60 / 60 / 24 / 365.25);
If you are using a UNIX-like system, you can use the system's date functionality to format times instead of reimplementing the PHP function:
date +%Y
gives the current year. You can then use the -d switch to format a custom date, rather than the current date:
date -d "UTC 1970-01-01 1287946333 secs +%Y"
gives "2010".
For years 1970 - 2038, you can use these equivalents (+/- a few minutes for the months and years):
Human readable time Seconds
1 minute 60 seconds
1 hour 3600 seconds
1 day 86400 seconds
1 week 604800 seconds
1 month (30.44 days) 2629743 seconds
1 year (365.24 days) 31556926 seconds
You can test your formulae here These equivalents can be off by enough minutes on key days (ie, Dec 31 / Jan 1) and are only good for epoch times away from boundaries.
If you want to be exact you need to deal with each and every leap year; either through a formula or through iteration.
This Perl code calculates the year from epoch seconds for any year +/- 130 or more years from 1970 (the Unix epoch). You need to know on your platform how big (32 bit or 64 bit) the epoch number is to know the span:
sub days_in_year {
my $year=shift;
my $leap =
($year % 400 == 0) ? 1
: ($year % 100 == 0) ? 0
: ($year % 4 == 0) ? 1
: 0
;
return (365+$leap);
}
sub epoch_to_year {
use integer;
my $t=shift;
my $ey=1970;
my $secs=$t;
if($t<0) {
while($secs<0) {
$secs+=days_in_year(--$ey)*24*60*60;
}
return $ey;
}
else {
while($secs>0) {
$secs-=days_in_year($ey++)*24*60*60;
}
return $ey if ($secs==0);
return $ey-1;
}
}
It is SLOW and you should use a library, but it you do not have one it will work. It is trivial to translate that to PHP. (sub => function, delete my, etc)