General Date Convertor and Calculator (Simple)

ardav ardav is offline Offline Jan 12th, 2009, 2:17 pm |
0
A simple function to change between common formats, e.g. UK, US, Unix dates and datetimes. Also allows the addition/subtraction of months, years, seconds etc etc. I have only set up a few formats in the function, but it can beeasily developed.
Quick reply to this message  
PHP Syntax
  1. function mod_date($date_in,$format_in,$y,$m,$d,$h,$i,$s,$format_out){
  2. /*
  3. Written by Alan Davies, 2009
  4. ============================
  5.  
  6. PARAMETERS
  7. ==========
  8. $date_in: STRING date or datetime in standard uk, unix or us format.
  9. $format_in: STRING u = unix, d = uk date, a = usa date
  10. $y: Year (INTEGER), negative, positive or 0
  11. $m: Month (INTEGER), negative, positive or 0
  12. $d: Day (INTEGER), negative, positive or 0
  13. $h: Hour (INTEGER), negative, positive or 0
  14. $i: Minute (INTEGER), negative, positive or 0
  15. $s: Second (INTEGER), negative, positive or 0
  16. $format_out: STRING u = unix, d = uk date, a = usa date,
  17. ut = unix date + time, dt = uk date + time, at = usa date + time
  18.  
  19. Example uses
  20. ============
  21. 1. converting a date from uk -> unix -> us and viceversa
  22. mod_date('23/11/2009','d',0,0,0,0,0,0,'u') would change uk date to unix
  23.  
  24. 2. adding or subtracting time to date
  25. mod_date('23/11/2009 13:00:00','d',1,0,0,-3,0,0,'ut') add one year, subtract 3 hours and place in unix format [2010-11-23 10:00:00]
  26. the function also corrects overflows (e.g. adding 13 months will actually add i year and 1 month)
  27. */
  28.  
  29.  
  30. $date_in = trim($date_in);
  31. $time_element = strstr($date_in," ");
  32. if($time_element){
  33. $datetime = explode(" ",$date_in);
  34. $date_aspect = $datetime[0];
  35. $time_aspect = $datetime[1];
  36. if(strstr($time_aspect,":")){
  37. $time_parts = explode(":",$time_aspect);
  38. $dh = $time_parts[0];settype($dh,"integer");
  39. $di = $time_parts[1];settype($dh,"integer");
  40. if(isset($time_parts[2])){
  41. $ds = $time_parts[2];settype($dh,"integer");
  42. }
  43. }
  44. }else{
  45. $date_aspect = $date_in;
  46. }
  47. if(!isset($dh) || $dh == "" || !is_int($dh))$dh = 0;
  48. if(!isset($di) || $di == "" || !is_int($di))$di = 0;
  49. if(!isset($ds) || $ds == "" || !is_int($ds))$ds = 0;
  50.  
  51. if(strstr($date_aspect,"/"))$date_parts = explode("/",$date_aspect);
  52. if(strstr($date_aspect,"-"))$date_parts = explode("-",$date_aspect);
  53. switch($format_in){
  54. case 'd':
  55. $dd = $date_parts[0];
  56. $dm = $date_parts[1];
  57. $dy = $date_parts[2];
  58. break;
  59. case 'u':
  60. $dd = $date_parts[2];$dm = $date_parts[1];$dy = $date_parts[0];
  61. break;
  62. case 'a':
  63. $dd = $date_parts[1];$dm = $date_parts[0];$dy = $date_parts[2];
  64. break;
  65. default:
  66. $error_msg = "You must provide a proper format for the input date: chose from 'u' (unix date/time), 'd' (UK date/time), 'a' (US date/time).";
  67. return $error_msg;
  68. break;
  69. }
  70.  
  71. $date = mktime($dh + $h,$di + $i,$ds + $s,$dm + $m,$dd + $d, $dy + $y);
  72.  
  73. switch($format_out){
  74. case 'd':
  75. $my_date = date('d/m/Y',$date);
  76. break;
  77. case 'u':
  78. $my_date = date('Y-m-d',$date);
  79. break;
  80. case 'a':
  81. $my_date = date('m/d/Y',$date);
  82. break;
  83. case 'dt':
  84. $my_date = date('d/m/Y H:i:s',$date);
  85. break;
  86. case 'ut':
  87. $my_date = date('Y-m-d H:i:s',$date);
  88. break;
  89. case 'at':
  90. $my_date = date('m/d/Y H:i:s',$date);
  91. break;
  92. default:
  93. $error_msg = "You must provide a proper format for output: chose from 'u' (unix date), 'd' (UK date), 'a' (US date), 'ut' (unix datetime), 'dt' (UK datetime), 'at' (US datetime).";
  94. return $error_msg;
  95. break;
  96. }
  97. return $my_date;
  98. }

Message:


Similar Threads
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC