Convert number to date

Asked

Viewed 2,679 times

2

Good afternoon,

I wonder if there is a way to convert number to date in PHP.

Example, in Excel the number 42873 is equivalent to date 18-05-2017. And I have how to do the opposite too.

Is there a function in PHP, to which I can make this conversion?

3 answers

1


Dates in Excel are stored in number of days from 1 January 1900, so for dates after this period you can create an object DateTime and format the date for display, example:

$n = 42873;
$dateTime = new DateTime("1899-12-30 + $n days");
echo $dateTime->format("d/m/Y");

Ideone example

Check out this reply on SOE

  • 1

    Thank you so much for your help @abfulan, I used your example in my code and it worked perfectly. Again, thank you so much!

  • @Tonyanderson Legal!

0

string date ( string $format [, int $timestamp ] )

The optional timestamp parameter is a Unix timestamp integer whose default is the local time if timestamp is not reported. In other words, the default is the value of the time function().

See more about the date.
More information in this reply.

-2

Some systems export dates to excel already in excel date format. Since Dates in Excel are stored in number of days from January 1, 1900, based on one of the answers to that topic.

I suggest using the CARBON library:

$dt_number = intVal($dado['data']);
if (is_numeric($dt_number)) {
    $dt = new Carbon('1899-12-30');
    $dt = $dt->addDays($dt_number);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.