Converting (mysql format) DAYS to timestamp..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Converting (mysql format) DAYS to timestamp..

Post by Jenk »

A friend is trying to use Day number, ala MySQL does, and wants to convert format using date()

i.e. he has an integer which is the number of days since 1st Jan 1A.D. e.g. 732889 is Tuesday 1st August 2006.

however.. there is no table interaction and there is 000's of iterations, as such using mysql_query() to calculate it is a no-no at the moment.

Does anyone know of a function to convert this to unix timestamp?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

There's nothing built into PHP that could convert it. You'll have to either find an elaborate library or build it yourself I'm afraid.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

dammit.

If only every year was 365days and every month 30days..
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If the data is coming from MySQL, alter the original selection. I'll assume that's not possible for whatever reason, so I would look into grabbing the source for MySQL and finding where the FROM_DAYS() function is written that way you can implement it in the same manor MySQL would handle it.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

That's what we have been 'discussing' for the last 45mins, me trying to convince him to retrieve the data as a timestamp in the first place.

However he is adamant there is no interaction with the DB for it, which leaves me wondering where the number comes from..

Either way he has just succumbed and will be using unix timestamp and work in seconds instead.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

There is a possibility of using strtotime() where you take the TO_DAYS() of the Epoch, diff the two then tell strtotime to give you the date from the epoch... ..that is if strtotime() wants to play nice. You could pick a date further into the future (from the epoch that is.)

Code: Select all

mysql> select to_days(from_unixtime(0));
+---------------------------+
| to_days(from_unixtime(0)) |
+---------------------------+
|                    719527 |
+---------------------------+
1 row in set (0.06 sec)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Thanks, I'm messing around with that now. However, when I run as you did above.. I get:

Code: Select all

mysql> select to_days(from_unixtime(0));
+---------------------------+
| to_days(from_unixtime(0)) |
+---------------------------+
|                    719528 |
+---------------------------+
1 row in set (0.00 sec)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You have to remember, my machine(s) are GMT-5 ;)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

So according to US TZ Jesus was born 5 hours late? :p

That's supposed to be the number of days between 1st Jan 1AD, to 1st Jan 1970. I would of thought TZ is relevant here?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

maybe http://de2.php.net/calendar is of interest
Hat
Forum Newbie
Posts: 1
Joined: Wed Nov 09, 2005 1:22 pm

I am that friend

Post by Hat »

Thank you all very much for all your help, comments and links.

I was just trying to get a "day number" expressed as an integer for any given date YYYY-mm-dd and to be able to switch between date and "day number" / "day number" and date at any time.. either using php or mysql and stumbled across the TO_DAYS and FROM_DAYS mysql - and thought this might give me what I need...

I think I have enough info now from your comments to be able to achieve what I need, so am going to chuck myself back into the various manuals and testing server for a bit

Much appreciated
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

^ being said friend :P
Post Reply