user3339256
user3339256

Reputation: 3

How to change a date to a General/numeric format number using PHP?

How can I convert today's date (m,d,Y) to a number (like in Excel). eg; today (2/21/2014) to Excel number (41690). I know how to get today's date, but don't know how to convert it.

Thanks

Upvotes: 0

Views: 311

Answers (3)

esdebon
esdebon

Reputation: 2729

Try this

<?php
$today=date('Y-m-d');// today;
$ts = strtotime($today); // today to seconds after 1970-01-01 (UNIX hour)
echo floor((($ts)/3600)/24)+25569;  // 25569 is the difference in second from 1970 to 1900
?> 

Upvotes: 0

Razvan
Razvan

Reputation: 2596

The Excel number value for the date represents the number of days since 01/01/1900.

So, you should use the following code (assuming GMT):

$today = '2014-02-21';

$datetime1 = new DateTime($today);
$datetime2 = new DateTime('1900-01-01');

$interval = $datetime2->diff($datetime1);
$excelDate = $interval->days + 1;

print "$today in Excel is: $excelDate";

Upvotes: 3

user2672373
user2672373

Reputation:

Perhaps you can try time() function which generates UNIX timestamp. You can convert the timestamp back to readable format using date() function

Upvotes: 0

Related Questions