Posted By

aamirrajpoot on 02/10/11


Tagged

date


Versions (?)

Some Date Functions


 / Published in: PHP
 

These are some date functions.

Get Current Week Start Date Get Current Week End Date Get Current Month Start Get Current Month End Get Current Year Start Get Current Year End

and these functions can help you getting last week's start date or next month end date.

To get last week's start date, remove 7 days from current week, and pass that date to get current week start date and it will return last week's start date.

  1. function week_start_date($wk_num, $yr, $first = 1, $format = 'l, d-M-y') {
  2. $wk_ts = strtotime('+' . $wk_num . ' weeks', strtotime($yr . '0101'));
  3. $mon_ts = strtotime('-' . date('w', $wk_ts) + $first . ' days', $wk_ts);
  4. return date($format, $mon_ts);
  5. }
  6.  
  7.  
  8.  
  9. function date_get_week_start($date, $date_format = "Y-m-d")
  10. {
  11. /*
  12.   * Week Start from Monday
  13.   */
  14. $year = date('Y', strtotime($date));
  15. $week = date('W', strtotime($date));
  16.  
  17. $date = week_start_date($week, $year, 1, $date_format);
  18.  
  19. return $date;
  20. }
  21.  
  22. function date_get_week_end($date, $date_format = "Y-m-d")
  23. {
  24. /*
  25.   * Week Ends on Sunday
  26.   */
  27. $year = date('Y', strtotime($date));
  28. $week = date('W', strtotime($date));
  29.  
  30. $date = week_start_date($week, $year, 7, $date_format);
  31.  
  32. return $date;
  33. }
  34.  
  35. function date_get_month_start($date, $date_format = "Y-m-d")
  36. {
  37. $year = date('Y', strtotime($date));
  38. $month = date('m', strtotime($date));
  39. $date = $year."-".$month."-"."01";
  40. return date($date_format, strtotime($date));
  41. }
  42.  
  43. function date_get_month_end($date, $date_format = "Y-m-d")
  44. {
  45. $year = date('Y', strtotime($date));
  46. $month = date('m', strtotime($date));
  47. return date($date_format,strtotime('-1 second',strtotime('+1 month',strtotime($month.'/01/'.$year.' 00:00:00'))));
  48. }
  49.  
  50. function date_get_year_start($date, $date_format = "Y-m-d")
  51. {
  52. $year = date('Y', strtotime($date));
  53. $date = $year."-01-01";
  54. return date($date_format, strtotime($date));
  55. }
  56.  
  57. function date_get_year_end($date, $date_format = "Y-m-d")
  58. {
  59. $year = date('Y', strtotime($date));
  60. $date = $year."-12-31";
  61. return date($date_format, strtotime($date));
  62. }

Report this snippet  

You need to login to post a comment.