0

I would like get the 1st date of week?
ie.

 if 2011-01-01 then 2011-01-01  
    if 2011-01-06 then 2011-01-01  
    if 2011-01-20 then 2011-01-14  
    if 2011-01-21 then 2011-01-21 

Sorry for misleading and making question confusion.

I just want to get the date of the 1st day of the week, ie. either 1 or 7 or 14 or 21 or 28

4
  • Do you consider Monday or Sunday as your first day of the week? It will make a difference to the answer. Feb 9, 2011 at 7:11
  • possible duplicate of Get date for monday and friday for the current week (PHP) Feb 9, 2011 at 7:31
  • You still haven't defined "first day of the week" properly. "1 or 7 or 14" doesn't make much sense.
    – deceze
    Feb 9, 2011 at 7:47
  • 2
    Weeks do not always start on 1, 7, 14, 21, 28 etc. The 1st of February was a Tuesday, for instance.
    – nico
    Feb 9, 2011 at 7:53

4 Answers 4

2

By far the most straight-forward way is to subtract the number of the day of the week (0 = Sunday for 'w', 1 = Monday for 'N') in days from the date:

$date = strtotime('2011-02-09');
$sunday = strtotime('-' . date('w', $date) . ' days', $date);
$monday = strtotime('-' . (date('N', $date) - 1) . ' days', $date);
2

If you consider monday as the first day of week

echo date('Y-m-d',strtotime("last monday", strtotime('tomorrow',strtotime($date))));

You can replace monday with any day of week.

2
  • 1
    It will fail if today is monday
    – zerkms
    Feb 9, 2011 at 7:29
  • 2
    I'd better prefer to use "tomorrow" feature: echo date('Y-m-d',strtotime("last monday", strtotime('tomorrow', strtotime($date)))); --- in this case you don't need to add another check.
    – zerkms
    Feb 9, 2011 at 7:40
0
$wk_day=date('w');
switch ($wk_day)
{
  case 0:  $diff = -1;        break;
  case 1:  $diff = 0;         break;
  default :$diff = $wk_day-1; break;
}
echo date('Y-m-d', time()-(86400*$diff));
1
  • If you consider Monday as your first day, then you should change the first line to this: $wk_day=date('N'); and the case should start at 1 and you should then change the case accordingly. Feb 9, 2011 at 7:12
0
$current_date =date('Y-m-d'); 

$year = date('Y');
$month = date('m');
$day = date('d');

$first_day = get_week_start($year, $month, $day);

$weeks_first_day =date('Y-m-d',$first_day);


    function get_week_start($year, $month, $day)
    {
    $timestamp = mktime(0, 0, 0, $month, $day, $year);
    return $timestamp = mktime(0, 0, 0, $month, date('d', $timestamp)-date('w', $timestamp), $year);
    }

you can get weeks first day from $weeks_first_day, you can also change format what ever !

here Sunday is the start date

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.