prince198 Posted July 9, 2008 Share Posted July 9, 2008 hi i look for function like to_days() of musql but in php for exemple in my sql to_days('2008-01-01') return 734407 i look for function in php put same result thank you for your helping Quote Link to comment Share on other sites More sharing options...
the shig Posted July 9, 2008 Share Posted July 9, 2008 hi, this'll do it. edit : keeps messing up format with   when editing see next post Quote Link to comment Share on other sites More sharing options...
the shig Posted July 9, 2008 Share Posted July 9, 2008 here it is <?php //takes a mysql date format yyyy-mm-dd function to_days($date) { $bits = explode('-', $date, 2); $year = $bits[0]; if(is_leap_year($year)) { $bits[0] = '2000'; } else{ $bits[0] = '1999'; } $date = implode('-',$bits); $leaps = 387; //leap years up to 1600 for($i = 1600; $i < $year; $i++) { if(is_leap_year($i)) { ++$leaps; } } $days = date('z', strtotime($date)); return $leaps + ($year * 365) + $days + 1; } function is_leap_year($year) { if($year % 100 == 0 && $year % 400 == 0) { return true; } if($year % 100 == 0) { return false; } if($year % 4 == 0) { return true; } return false; } ?> this works for dates after 1600-01-01. for earlier dates change:- <?php $leaps = 387; //leap years up to 1600 for($i = 1600; $i < $year; $i++) { if(is_leap_year($i)) { ++$leaps; } } ?> to <?php $leaps = 0; for($i = 1; $i < $year; $i++) { if(is_leap_year($i)) { ++$leaps; } } ?> hope this helps Quote Link to comment Share on other sites More sharing options...
matthewhaworth Posted July 9, 2008 Share Posted July 9, 2008 Erm, I imagine the strtotime(); function could help.. <?php $date = "08/23/1990"; $days = strtotime($date) / (60 * 60 * 24); ?> Or you could make a function of it if you wish <?php function to_days($date) { $r = strtotime($date) / (60*60*24); return $r; } So the first code would then be <?php $days = to_days("08/23/1990"); ?> I tried it with your sample data - didn't work lol, sorry lol. Quote Link to comment Share on other sites More sharing options...
discomatt Posted July 9, 2008 Share Posted July 9, 2008 Erm, I imagine the strtotime(); function could help.. <?php $date = "23/08/1990"; $days = strtotime($date) / (60 * 60 * 24); ?> But that's days since 1970, not exactly what the OP wants Quote Link to comment Share on other sites More sharing options...
matthewhaworth Posted July 9, 2008 Share Posted July 9, 2008 Erm, I imagine the strtotime(); function could help.. <?php $date = "23/08/1990"; $days = strtotime($date) / (60 * 60 * 24); ?> But that's days since 1970, not exactly what the OP wants Tbh, I foolishly never looked at what the function he wanted actually did and just took a guess lol.. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 9, 2008 Share Posted July 9, 2008 try <?php $date = "23/08/1990"; $days = round((strtotime($date)-date("U")) / (60 * 60 * 24)); ?> Quote Link to comment Share on other sites More sharing options...
discomatt Posted July 9, 2008 Share Posted July 9, 2008 try <?php $date = "23/08/1990"; $days = round((strtotime($date)-date("U")) / (60 * 60 * 24)); ?> Again, check what the OP wants And just for future reference, you can use time() instead of date("U") Quote Link to comment Share on other sites More sharing options...
Barand Posted July 9, 2008 Share Posted July 9, 2008 Erm, I imagine the strtotime(); function could help.. <?php $date = "23/08/1990"; $days = strtotime($date) / (60 * 60 * 24); ?> But that's days since 1970, not exactly what the OP wants with a slight adjustment, given that SELECT TO_DAYS('1969-12-31') --> 719527 <?php $days = to_days("23/08/1990"); function to_days ($date) { $days = (strtotime($date) / (60 * 60 * 24)); return 719527 + $days; } ?> Quote Link to comment Share on other sites More sharing options...
prince198 Posted July 9, 2008 Author Share Posted July 9, 2008 thak you so very much for your help. i was busy all day and i din't view my profil i will try all your solution and i will tell you if that's work or not tomorrow thank you so very much Quote Link to comment Share on other sites More sharing options...
discomatt Posted July 9, 2008 Share Posted July 9, 2008 <?php $days = to_days("23/08/1990"); function to_days ($date) { $days = (strtotime($date) / (60 * 60 * 24)); return 719527 + $days; } ?> Would work extremely well assuming the dates entered were after 1969. Quote Link to comment Share on other sites More sharing options...
Barand Posted July 9, 2008 Share Posted July 9, 2008 Well, apart from the the offset being a day out (should be 719528) it works fine for my DOB [pre] mysql> SELECT TO_DAYS('1949-01-22'); +-----------------------+ | TO_DAYS('1949-01-22') | +-----------------------+ | 711879 | +-----------------------+ 1 row in set (0.02 sec) [/pre] <?php $days = to_days("01/22/1949"); echo $days; // --> 711879 function to_days ($date) { $days = (strtotime($date) / (60 * 60 * 24)); return 719528 + $days; } ?> Quote Link to comment Share on other sites More sharing options...
discomatt Posted July 9, 2008 Share Posted July 9, 2008 I was unaware strtotime could produce a negative integer... that's great to know! Quote Link to comment Share on other sites More sharing options...
Barand Posted July 9, 2008 Share Posted July 9, 2008 The Unix epoch is 1970-2037, 67 years, so the lower bound will be somewhere in 1903 (1970 minus 67) EDIT: I just checked the manual - the earliest date would be 13 Dec 1901 Note: The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer.) Additionally, not all platforms support negative timestamps, therefore your date range may be limited to no earlier than the Unix epoch. This means that e.g. dates prior to Jan 1, 1970 will not work on Windows, some Linux distributions, and a few other operating systems. PHP 5.1.0 and newer versions overcome this limitation though. Quote Link to comment Share on other sites More sharing options...
discomatt Posted July 9, 2008 Share Posted July 9, 2008 Would work extremely well assuming the dates entered were after 1900 There we go Thanks for the bit of knowledge there, Barand Quote Link to comment Share on other sites More sharing options...
prince198 Posted July 10, 2008 Author Share Posted July 10, 2008 hi everybody i tried all function first function that ( the shig ) had writen work exactly for the rest don't work and don't put the result exactly some functions turn float result like 733405.333333 i know there is round in php but its possible turn not result like mysql thank a lot for all your help and specially "the shig " Quote Link to comment Share on other sites More sharing options...
prince198 Posted July 10, 2008 Author Share Posted July 10, 2008 function of Barand work but need round function and i'm afraid for exemple my sql turn for date 45555 and this function turn 45554.1111 so round will trun 45554 and not 45555 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.