Posted By

JonnySnip3r on 01/10/11


Tagged

php date function


Versions (?)

Convert Pretty Dates with PHP


 / Published in: PHP
 

This takes a date like so: Sat, 08 Jan 2011 from a database and allows you to use it in your code. For example if you wanted to work out a week from Sat, 08 Jan 2011 it can be done with ease. I created this for a project i am currently working on. There maybe an easier way to deal with this problem but its works so enjoy!

  1. <?php
  2.  
  3. function scheduleGameForNextWeek($date)
  4. {
  5. $day = substr($date,5,-9);
  6. $month = substr($date,-8,3);
  7. $year = substr($date, -4);
  8.  
  9. $month = strtolower($month);
  10.  
  11. switch($month)
  12. {
  13. case "jan":
  14. $month = "01";
  15. break;
  16. case "feb":
  17. $month = "02";
  18. break;
  19. case "mar":
  20. $month = "03";
  21. break;
  22. case "apr":
  23. $month = "04";
  24. break;
  25. case "may":
  26. $month = "05";
  27. break;
  28. case "jun":
  29. $month = "06";
  30. break;
  31. case "jul":
  32. $month = "07";
  33. break;
  34. case "aug":
  35. $month = "08";
  36. break;
  37. case "sep":
  38. $month = "09";
  39. break;
  40. case "oct":
  41. $month = "10";
  42. break;
  43. case "nov":
  44. $month = "11";
  45. break;
  46. case "dec":
  47. $month = "12";
  48. break;
  49. default:
  50. $month = "00";
  51. break;
  52. }
  53.  
  54. return $newDate = date('F jS Y', mktime(0,0,0,$month,$day+7,$year));
  55. }
  56.  
  57. $date = "Sun, 9 Jan 2011";
  58.  
  59. echo $newDate = scheduleGameForNextWeek($date);
  60.  
  61. ?>

Report this snippet  

You need to login to post a comment.