Convert PHP date to Excel? - php

Excel's date format is, I believe, the # of days since December 30, 1899. (Why? Because it's based on 1/1/1900 but they erroneously include a leap day in 1900, and it's one-based. So it's fine for the first couple of months in 1900 but then it goes wrong. 1/1/1901 in Excel is "367".)
So, how can I convert a date or timestamp from PHP (which is typically stored as a Unix timestamp, the # of seconds since 1/1/1970) to Excel?
In PHP 5.1.6?
Yeah, bet you didn't see that one coming. So I don't have any of the DateTime objects, date_create(), date_diff(), etc. functions available to me.
Is it even possible without simply recording the Excel day for 1/1/1970 and working from there? And is there a DST-proof version?

If I'm not mistaken, there are 86400 seconds in a day, and 25569 days between 30 Dec 1899 and 01 Jan 1970. So, to convert a Unix timestamp to an Excel date, the formula
ExcelDate = 25569 + UnixTS / 86400
ought to work. Of course, this formula is only correct for UTC. (Also, it ignores leap seconds.) For other timezones, if you know the offset (in seconds!) from UTC, you can just add it to the timestamp before using the formula above.

Related

Convert date format 1542575966120 18/11/2018 [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 4 years ago.
I have a DB that shows when the user made the last login but then it shows 1542575966120. I wanted it to show so 18/11/2018 19:00
I tried using this in php
$intDate = "20". $ infologado ["lastlogin"];
$newDate = date ("d-m-Y", strtotime ($ intDate));
but I could not.
sorry for English
So as #Taha Paksu had mentioned, these numbers are a timestamp (seconds since 1 January 1970). Try this code:
$intDate = 1542575966120;
$newDate = date('d/m/Y H:i', $intDate/1000);
It is in miliseconds, date function accepts seconds, thus the division by 1000. Also no need to put it into strtotime, because this function is meant to convert string dates to... said numeric timestamps.
In your case, you can put $intDate = $infologado['lastlogin']; instead of first line to get the result dynamically from the database.
First of all, you need to learn what a timestamp is. The timestamp is a number which shows the seconds passed (or milliseconds, some include the milliseconds too) since epoch (01/01/1970). A general definition can be found here:
The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting leap seconds (in ISO 8601: 1970-01-01T00:00:00Z). Literally speaking the epoch is Unix time 0 (midnight 1/1/1970), but 'epoch' is often used as a synonym for 'Unix time'. Many Unix systems store epoch dates as a signed 32-bit integer, which might cause problems on January 19, 2038 (known as the Year 2038 problem or Y2038).
The converter on this page converts timestamps in seconds, milliseconds and microseconds to readable dates.
Taken from: https://www.epochconverter.com/ a tool which you can convert your dates to/from timestamps or vice versa.
Then to answer your question, the system saved the dates as a timestamp to the database to (probably) bypass the formatting errors on each different system that uses it.
Nevermind, TL;DR:
The number shows Sunday, 18 November 2018 21:19:26.120 when you give it to the timestamp converter I mentioned above. With PHP, you can use:
$unixTimestamp = 1542575966120;
$dt = DateTime::createFromFormat("U.u", $unixTimestamp / 1000);
var_dump($dt);
to convert to PHP DateTime class, then you can use it in your application.

Dates in PHP 4.4

I am working for this association in my school and I am migrating web applications since, as of today, they are still running in PHP 4.4... But, I would like to quickly implement some fixes before changing to a newer PHP version.
One of these changes is related to dates comparison. The problem is I don't have strtotime and stuff like that, they all came in PHP 5.x.
I have to check if a timestamp is in one of two time intervals. In concrete:
I have a timestamp for when a person adhered to the association, school year goes from September 1st of year N to July 31st of year N+1, and I must check if the timestamp (and also the current time) lies
Between September 1st of year N and January 31st of year N+1, or
Between January 1st of year N+1 and July 31st of year N+1
So the question is what is the best way to do this? I came up with a solution: create my own timestamps and then compare. To create a timestamp for year N, I multiply the number of years since epoch time (N - 1970) by the number of seconds in a year ( 365.25 * 24 * 60 * 60 ). Then to that timestamp I add the number of seconds to September 1st, to January 1st, and so on. Finally I compare the whole. Isn't there a better way?
You are able to use PHP's strtotime function in PHP 4.4:
http://php.net/manual/en/function.strtotime.php
strtotime
(PHP 4, PHP 5, PHP 7)
strtotime — Parse about any English textual datetime description into a Unix timestamp
Here is an online example where you can run strtotime in 4.4.9:
http://sandbox.onlinephpfunctions.com/code/38559a58820d08105f746691bf338c565c1ed4e0

PHP mktime and timezone

I'm working on some time related features and I opt to always use UTC times and store time stamps as integers for consistency.
However, I noticed that when I use mktime it seems that the currently set time zone has an influence of the return value of mktime. From the documentation I understand that mktime is supposed to return the number of seconds since epoch:
Returns the Unix timestamp corresponding to the arguments given. This
timestamp is a long integer containing the number of seconds between
the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.
http://php.net/manual/en/function.mktime.php
However, it seems that mktime is including the time zone that is currently set. When using the following code:
date_default_timezone_set('UTC');
$time = mktime(0, 0, 0, 1, 1, 2016 );
echo "{$time}\n";
date_default_timezone_set('Australia/Sydney');
$time = mktime(0, 0, 0, 1, 1, 2016 );
echo "{$time}\n";
I would expect the two time vales to be same but apparently they are not:
1451606400
1451566800
Which seems to be exacly an 11 hour difference:
1451606400 - 1451566800 = 39600 / (60*60) = 11
What do I not understand correctly about mktime and/or why is the time zone taken into account when using mktime?
I can't tell you why it is the way it is (PHP has never made sense to me when it comes to date and time) but there is an alternative function gmmktime() which is
Identical to mktime() except the passed parameters represents a GMT date. gmmktime() internally uses mktime() so only times valid in derived local time can be used.
There is also a comment on the PHP documentation for this function which explains how mktime(), gmmktime() and time() work. Essentially, they assume that you always think in time zones even if a UNIX timestamp itself doesn't carry a timezone.
Resulting Unix timestamp are indeed encoded in a timezone agnostic way, but input arguments are interpreted relative to the timezone set for current process. And indeed, Sidneys 2016-01-01 00:00:00 (GMT+11) happened 11 hours before UTC 2016-01-01 00:00:00.
When some foreigner tells you a time, you have to know its time zone to correctly interpret it, and so does mktime().
If dates you want to pass to mktime() are UTC dates, then use gmmktime() which exists for that purpose.

When converting a string to time with strtotime() in PHP it adds one hour, why?

When I am converting date using strtotime() function it automatically added 1 hour in existing date.
For example:
$PublishDate = "1/13/2012 **17**:0";
echo strtotime($PublishDate);
// OUTPUT : 1/13/2012 **18**:0
Why 1 hour is incremented automatically?
Something odd is going on here. strtotime() converts attempts to convert a string representation of a date to the Unix time format (see the strtotime() documentation). Unix time is the total number of seconds since January 1st, 1970. This ends up being a large number, such as 1326723022 (the current Unix time as of writing). You can read more about Unix time here.
strtotime() should ALWAYS return either a 32 bit integer or FALSE (-1 in older versions of php).

Why does strtotime give different result in different timezone?

I am not sure why strtotime() in PHP returns different result in different timezone even though same date is given as parameter, does anyone know the answer? I also want to know, can I do similar task (converting a datetime to an int to do calculations easily) with another function which gives same result across different timezone?
EDIT:
An example:
If I use strtotime('2011-09-19 00:00:00') shouldn't it just return the difference between 'January 1 1970 00:00:00' and '2011-09-19 00:00:00' in seconds ? Why timezone is an issue here? And can I get something which gives just difference without timezone issue?
In short: time zone is considered because the Unix Epoch value is considered in GMT.
In broader sense 2011-09-19 00:00:00 comes to Bangladesh almost after 6 hours it is 2011-09-19 00:00:00 in GMT zone. Because of this gap, another 21600 seconds have passed in the GMT zone when the same date appears in BD.
Since the calculation is done in respect to the GMT, you have to add these 21600 seconds to get the actual difference.
strtotime gives different results in different timezones because it takes timezones into account...
From strtotime's manual:
The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC)
This function will use the TZ environment variable (if available) to calculate the timestamp. Since PHP 5.1.0 there are easier ways to define the timezone that is used across all date/time functions. That process is explained in the date_default_timezone_get() function page.
Have a look at mktime().
Since PHP 5.1, you can use date_default_timezone_set before calling mktime or strtotime.
From the PHP manual:
This function will use the TZ environment variable (if available) to calculate the timestamp. Since PHP 5.1.0 there are easier ways to define the timezone that is used across all date/time functions. That process is explained in the date_default_timezone_get() function page.
http://php.net/manual/en/function.strtotime.php
Use date_default_timezone_set before calling date/time functions to choose which time zone you want to work in.
http://www.php.net/manual/en/function.date-default-timezone-set.php
From PHP docs on strtotime:
This function will use the TZ environment variable (if available) to
calculate the timestamp. Since PHP 5.1.0 there are easier ways to
define the timezone that is used across all date/time functions. That
process is explained in the date_default_timezone_get() function page.
Try setting your own time zone.
i think probably there will be one time of each php programmer that this function will make him wants to really understand how actually php works with date and time functions.
funny this function when you try something like...err...for example, assume that today at this very moment is July 11th, 2012 at 13:00:00 (2012-07-11 13:00:00) and then you try strtotime to find exactly the same moment of the day but for tomorrow:
$x = strtotime('2012-07-12 13:00:00');
$y = strtotime('+1 Day');
$z = $x-$y;
$x and $y of the above first 2 lines will not return the same thing even you ignore the minute and second counts but $z will be around 25200 or around 7 hours in the difference between this 2 lines if your sever is in somewhere of the USA that the GMT is -5 hours but you browser calls this function from berlin in summer where the GMT is +2 hours... LOL now you can get the idea how php work with this function ;)

Categories