Mike SD Posted May 8, 2009 Share Posted May 8, 2009 Hello all Does anyone know how to use PHP to convert a serial date into a normal date format like dd/mm/yyyy? Quote Link to comment https://forums.phpfreaks.com/topic/157341-convert-serial-date-into-ddmmyyyy/ Share on other sites More sharing options...
ILMV Posted May 8, 2009 Share Posted May 8, 2009 Is it just me that needs clarification, what's a serial date? Have you got an example / format? Quote Link to comment https://forums.phpfreaks.com/topic/157341-convert-serial-date-into-ddmmyyyy/#findComment-829310 Share on other sites More sharing options...
JonnoTheDev Posted May 8, 2009 Share Posted May 8, 2009 Try this <? function convertSerialDate($date) { $timestamp = ($date - 25569) * 86400; return date("d/m/Y",$timestamp); } print convertSerialDate(733535); ?> Quote Link to comment https://forums.phpfreaks.com/topic/157341-convert-serial-date-into-ddmmyyyy/#findComment-829313 Share on other sites More sharing options...
Mike SD Posted May 8, 2009 Author Share Posted May 8, 2009 Try this <? function convertSerialDate($date) { $timestamp = ($date - 25569) * 86400; return date("d/m/Y",$timestamp); } print convertSerialDate(733535); ?> Fantastic I have been searching the internet all morning for an answer to this one Thank you very much Quote Link to comment https://forums.phpfreaks.com/topic/157341-convert-serial-date-into-ddmmyyyy/#findComment-829314 Share on other sites More sharing options...
Mike SD Posted May 8, 2009 Author Share Posted May 8, 2009 Is it just me that needs clarification, what's a serial date? Have you got an example / format? A serial date is a date with a general formatting in Excel 08/05/2009 = 39941 Quote Link to comment https://forums.phpfreaks.com/topic/157341-convert-serial-date-into-ddmmyyyy/#findComment-829315 Share on other sites More sharing options...
Mike SD Posted May 8, 2009 Author Share Posted May 8, 2009 Try this <? function convertSerialDate($date) { $timestamp = ($date - 25569) * 86400; return date("d/m/Y",$timestamp); } print convertSerialDate(733535); ?> I was wondering Is it possible to convert from dd/mm/yyyy format back to a serial date? Quote Link to comment https://forums.phpfreaks.com/topic/157341-convert-serial-date-into-ddmmyyyy/#findComment-829325 Share on other sites More sharing options...
JonnoTheDev Posted May 8, 2009 Share Posted May 8, 2009 Of course, just requires the correct formula. Quote Link to comment https://forums.phpfreaks.com/topic/157341-convert-serial-date-into-ddmmyyyy/#findComment-829327 Share on other sites More sharing options...
Mike SD Posted May 8, 2009 Author Share Posted May 8, 2009 Of course, just requires the correct formula. Thanks How would that formula go? Ive tried creating one based on the original formula you provided, but I was unsuccesfull (sorry if im being cheeky) Quote Link to comment https://forums.phpfreaks.com/topic/157341-convert-serial-date-into-ddmmyyyy/#findComment-829334 Share on other sites More sharing options...
thebadbad Posted May 8, 2009 Share Posted May 8, 2009 Reversing neil.johnson's function, you get: <?php function date2serial($date) { //convert dd/mm/yyyy format to yyyy-mm-dd format $date_arr = explode('/', $date); $std_date = implode(array_reverse($date_arr), '-'); $timestamp = strtotime($std_date); return round(($timestamp + 25569 * 86400) / 86400); } ?> Edit: But I'm not sure if round() gives us the desired serial for every date. Edit 2: And some quick calculation leaves us with the simpler return round(($timestamp / 86400) + 25569); instead of the current return line Quote Link to comment https://forums.phpfreaks.com/topic/157341-convert-serial-date-into-ddmmyyyy/#findComment-829339 Share on other sites More sharing options...
Mike SD Posted May 8, 2009 Author Share Posted May 8, 2009 Reversing neil.johnson's function, you get: <?php function date2serial($date) { //convert dd/mm/yyyy format to yyyy-mm-dd format $date_arr = explode('/', $date); $std_date = implode(array_reverse($date_arr), '-'); $timestamp = strtotime($std_date); return round(($timestamp + 25569 * 86400) / 86400); } ?> Edit: But I'm not sure if round() gives us the desired serial for every date. Thank you so much This has made me very very happy I have run a test and it converted the data back to a serial date successfully I will add it to my script and let you know if I encounter any problems Thank you Quote Link to comment https://forums.phpfreaks.com/topic/157341-convert-serial-date-into-ddmmyyyy/#findComment-829344 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.