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

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

Related

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
}

Wrong php date('t')

I have this script
echo 'giorni mese: '.date('t', $mese_start).' - mese start: '.$mese_start;
output is:
giorni mese: 31 - mese start: 11
But november doesn't have 30 days?
What am I missing?
Update:
right, thank you.
The second argument to date() is a timestamp, which is the number of seconds since 1970-01-01 00:00:00 UTC. The value of $mese_start is 11. So that timestamp is 1970-01-01 00:00:11 UTC, and January has 31 days.
If you want to use $mese_start as a month number rather than a timestamp, you can use mktime() to create a timestamp from a particular date:
$ts = mktime(0, 0, 0, $mese_start);
echo 'giorni mese: '.date('t', $ts).' - mese start: '.$mese_start;
The second argument of date is interpreted as a Unix timestamp, not a month. You are passing "11" which equals some time on January 1st, 1970. And January has 31 days.
You could either construct a valid timestamp for November or use cal_days_in_month:
echo cal_days_in_month(CAL_GREGORIAN, 11, 2018); // 30

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.

How can I add two unix timestamps together?

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

How to convert week number and year into unix timestamp?

I'm trying to group together dates into a week number and year, and then I want to convert that week number back into a unix timestamp. How can I go about doing this?
I assume you are using ISO 8601 week numbers, and want the first day of a ISO 8601 week so that e.g. Week 1 of 2011 returns January 3 2011.
strtotime can do this out of the box using the {YYYY}W{WW} format:
echo date("Y-m-d", strtotime("2011W01")); // 2011-01-03
Note that the week number needs to be two digits.
Shamefully, DateTime::createFromFormat, the fancy new PHP 5 way of dealing with dates, seems unable to parse this kind of information - it doesn't have a "week" placeholder.
$week: The week number
$year: The year number
Then:
$timestamp = gmmktime (0, 0 , 0 , 1, , 4 + 7*($week - 1), $year);
The 4 + 7*($week - 1) comes from the fact that according to ISO 8601, the first week of the year is the one that contains January 4th.
strtotime('1/1/2011 + 4 weeks') (1/1 ist always in week number one; this would bring me to week number five). if you want any timestamp in the week then that's all you need, else you would have to go to the monday in this week:
$t = strtotime('1/1/2011 + 4 weeks');
$t -= 24 * 60 * 60 * date('w', $t);
Update: Instead of 1/1/2011 use the first monday in 2011. The 2nd calculation is not needed anymore.

Categories