Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the unix timestamp formula?

First of all, i know this question has been sort of asked/sort-of answered here: Calculate day number from an unix-timestamp in a math way? .

I need a custom function/formula for this. so it only returns a ISO format date. "YYYY-MM-DD".

eg. 1316278442 = 2011-09-17

EDIT by Ext! THIS IS WRONG ! Please don't read this.

I've been at this all day! The only thing i managed to get out is the day of the week.

$dayOfWeek=($timestamp/86400)%7; //And here 1 is Saturday, 7 is Friday

Speed is the issue, that is why i don't want to use date('Y-m-d',$timestamp);

If you cannot help me whit a custom function or formula, at least give me a better explanation on how to do this. It was done in so many languages, there must be someone out there that knows how to do this.

Thank you in advance for your help.

like image 415
Ext Avatar asked Sep 17 '11 15:09

Ext


1 Answers

Here is the function that date() and DateTime::setTimestamp() use to compute the date from a unix timestamp:

https://github.com/php/php-src/blob/d57eefe6227081001978c3a63224065af8b5728e/ext/date/lib/unixtime2tm.c#L39

As you can see, this is a bit complicated by leap years, etc.

--

That said, if you need only the day of the week, it seems that you can safely ignore leap years, and just use the formula you given in the question: $dayOfWeek=($timestamp/86400)%7

like image 80
Arnaud Le Blanc Avatar answered Sep 25 '22 16:09

Arnaud Le Blanc