How can I add two unix timestamps together? - php

Given that I have 2 timestamps:
1332954000
which is 18:00pm in human readable format. I got this from strtotime("18:00")
and
1330992000
which is Tues 6 march 2012 in human readable format
How can I add them together such that it will become Tuesday 6 March 2012 18:00pm in Unix timestamp format?

1332954000 doesn't mean 18:00, it means 1332954000 seconds from 1st Jan 1970.
You can't represent 18:00 in seconds from 1st Jan 1970, so your question is meaningless.
You could represent 18:00 as being equal to 18 * 60 * 60 = 64800 seconds, then add that on to your date, which would make sense.
You could concatenate the strings together then use strtotime on that to get what you want alternatively.

Solution 1
$Var1 = '18:00'; // Not 18:00pm please....
$Var2 = 'Tues 6 march 2012';
$NewTimeStamp = date('U', strtotime($Var1.' '.$Var2));
Solution 2 - Not sure
$Var1 = strtotime('18:00pm') - time();
$Var2 = strtotime('Tues 6 march 2012');
$NewTimeStamp = date('U', $Var1 + $Var2);
Be sure to go read about function.date to know how to format the time but U will give you timestamp.
Also, 18:00pm ain't really a time ... 6:00PM is or 18:00 without PM

The timestamp is just the number of seconds from the 1st of January, 1970. Assuming:
1330992000
Is the correct timestamp for Tues 6 march 2012 at 12:00 AM, then all you need to do is add 18 hours, in seconds to the timestamp.
$timestamp + 64800 = $final_timestamp

Related

phpExcel given timestamp instead of actual date in excel sheet [duplicate]

I want to read dates from an Excel File, but when I print
the dates to the screen I can only see one number instead Of
the date, why?
Excel file:
Result:
Excel treats dates as numbers, with the number representing the number of days after December 31 1899. So day 1 is January 1 1900, and day 43102 is January 3 2018. But wait... your data says January 2 2018! It turns out that Microsoft thinks that 1900 was a leap year and so day 60 is February 29 1900 when in the real world it was actually March 1. Anyway, what that means is that for dates after February 28 1900, you need to subtract one from the day number to get the correct date. So, to convert an Excel day number to a date in PHP, you use the following code:
$dayval = 43102; // you would read from your file here
$date = new DateTime('1899-12-31');
$date->modify("+$dayval day -1 day");
echo $date->format('Y-m-d');
Output:
2018-01-02
Please use this formula to change from Excel date to Unix date, then you can use "gmdate" to get the real date in PHP:
UNIX_DATE = (EXCEL_DATE - 25569) * 86400
and to convert from Unix date to Excel date, use this formula:
EXCEL_DATE = 25569 + (UNIX_DATE / 86400)
After putting this formula into a variable, you can get the real date in PHP using this example:
$UNIX_DATE = ($EXCEL_DATE - 25569) * 86400;
echo gmdate("d-m-Y H:i:s", $UNIX_DATE);
The 86400 is number of seconds in a day = 24 * 60 * 60. The 25569 is the number of days from Jan 1, 1900 to Jan 1, 1970. Excel base date is Jan 1, 1900 and Unix is Jan 1, 1970. UNIX date values are in seconds from Jan 1, 1970 (midnight Dec 31, 1969). So to convert from excel you must subtract the number of days and then convert to seconds

Time duration remaining unix timestamp

I have a unix timestamp and a duration, such as a year. I'd like to find out how to determine how much time is remaining from the duration based off the start unix timestamp. I want it in monthes.
I imagine its something like:
duration convert to seconds.
minus start to today to seconds (elapsed)
minus elapsed from duration
convert remaining to remaining in seconds
change remaining to monthes and determine remainder to days
I'm sure thouhg theres a php solution which is more accurate and precise than my design i think.
UPDATE
I have a unix timestamp of 1564113711, which is July 26, 2019. Today is August 7,2019. I need to know how much time remains in monthes and days from July 26,2020 (one year from the start) to today, August 7, 2019, where the elapsed time is July 26,2019 to August 7,2019
The DateTime object and DateTime::diff have everything you need.
$timeStamp = 1564113711; //26. Juli 2019
$duration = "1 Year";
$endDate = date_create("#".$timeStamp)->modify($duration);
$diff = date_create("today UTC")->diff($endDate);
//output
if($diff->invert) {
echo "your time's up";
}
else {
echo $diff->y * 12 + $diff->m." Month and ".$diff->d. " Days";
//e.g. 10 Month and 24 Days
}

PHP strtotime returns whole year instead of a single day

I am currently using PHP ROUND , ABS , STRTOTIME to calculate the difference between two dates.
The calculation works until you select a $_SESSION['b_checkout'] from a new year. i.e. if the b_checkin is Dec 30 and the b_checkout is Dec 31, this returns the correct $no_nights as 1 day.
$_SESSION['b_checkin'] and $_SESSION['b_checkout'] are using 'D F jS Y' date format. e.g.
$_SESSION['b_checkin'] = "Wednesday, 31 December, 2014"
$_SESSION['b_checkout'] = "Thursday, 1 January, 2015"
$no_nights = round(abs(strtotime($_SESSION['b_checkout']) - strtotime($_SESSION['b_checkin']))/(60*60*24));
Currently this outputs (echo $no_nights) 363 days instead of 1 day. What is the problem?
Remove all the commas and see it works!
$b_checkin = "Wednesday 31 December 2014";
$b_checkout = "Thursday 1 January 2015";
$no_nights = round(abs(strtotime($b_checkout) - strtotime($b_checkin))/(60*60*24));
echo $no_nights;
When php can't interpret the year correctly, it uses the current year for parsing. thats how you are getting 363 days as result.
Use below format for checkin and checkout
// 'F jS Y'
$_SESSION['b_checkin'] = "31 December, 2014";
$_SESSION['b_checkout'] = "1 January, 2015";
Hope this helps ;)
Cheers!!!

PHP - gmdate() and Large Integers

I'm getting a result I'm not expecting from gmdate()
<?php
$secs = 175707;
echo gmdate("H:i:s", $secs); // result: 00:48:27
?>
The result is 00:48:27, which is way off. It appears the hours is getting pushed down a position. Am I suspecting that right?
gmdate works on dates, not periods of time. In other words, your timestamp is being interpreted as a point in time early in January 3rd of 1970 (specifically 00:00:00 1 Jan 1970 + 15707 seconds = 00:48:27 3 Jan 1970). This is where your 00:48:27 comes from.
gmdate (and date) are not meant to be used this way. If you just want to calculate hours/minutes/seconds based on number of seconds, calculate them directly:
$seconds = 175707;
$hours = floor($seconds / 3600);
$seconds -= $hours * 3600;
$minutes = floor($seconds / 60);
$seconds -= $minutes * 60;
echo "$hours:$minutes:$seconds"; //48:48:27
Make note that this does not work with civil days. This is because of daylight saving time. A day is not always 24 hours. Sometimes it's 23, and sometimes its 25 when DST is coming into effect or ending. With days as a unit of measure (i.e. a day is always 24 hours), this does work. As an example, 10 March is 23 hours and 3 Nov is 25 hours in the United States. If you are happy with static 24 hours days, then the same approach will of course work.
gmdate() expects a UNIX timestamp - seconds since Jan 1/1970. You've passed in the equivalent of Saturday, Jan 3rd, 1970, 12:48:27am.
e.g. try this:
echo gmdate('r', 175707);
From the manual
the time returned is Greenwich Mean Time (GMT)
So if you're in CET (GMT +1) it indeed will give you an hour of 0.
Have a look at this if you want to see where you are going wrong:
echo date("d-M-Y H:i:s", 175707);
That will give an output of 03-Jan-1970 00:48:27. i.e. 175707 seconds from 00:00:00 on the 1st of January 1970 = 03-Jan-1970 00:48:27.

Bizzare PHP behaviour calculating time differences, only on 6th and 7th September, 2013

I'm trying to calculate the difference in days between two dates. I'm getting bizzare behaviour - I've narrowed it down to 6th and 7th October, 2013, as you can see below. Whenever the date range spans those dates, the calculation is a day out.
// WRONG! current year - 2013
$datediff = strtotime('2013-10-07') - strtotime('2013-10-06');
$startToEndDays = floor($datediff/(60*60*24));
print_r($startToEndDays); // Outputs 0 - should output 1
// RIGHT! next year - 2014
$datediff = strtotime('2014-10-07') - strtotime('2014-10-06');
$startToEndDays = floor($datediff/(60*60*24));
print_r($startToEndDays); // Outputs 1 - correct
Any idea what could be the issue here?
haha OK, it turns out 6th/7th October 2013 is when daylight savings starts in Sydney, Australia. So, the number of hours between those dates is calculated (correctly) as 23. But, 23 hrs is not quite a day.
If you're using PHP 5.3+, then this is how you should calculate the difference between dates in days, to save yourself any daylight savings headaches:
$startDate = new DateTime('2013-10-07');
$endDate = new DateTime('2013-10-06');
$interval = $startDate->diff($endDate);
$days = $interval->days;

Categories