How can i get a week start and end in php?

This is how you can get the week starting date or ending date by giving a week number and the year.


$monday   
= date('Ymd', strtotime("2013-W01-1"));
$sunday    = date('Ymd', strtotime("2013-W01-7"));

?>

/**
 * Get starting date from week number and year
 * Monday is first day of week
 *
 * @param unknown_type $wk_num
 * @param unknown_type $yr
 * @param unknown_type $first
 * @param unknown_type $format
 * @return unknown
 */
function week_start_date($week, $year, $format = 'Ymd', $date = FALSE) {
   
    if (
$date) {
       
$week = date("W", strtotime($date));
       
$year = date("o", strtotime($date));
    }

   
$week = sprintf("%02s", $week);

   
$desiredMonday = date($format, strtotime("$year-W$week-1"));

    return
$desiredMonday;
}

$sStartDate = week_start_date($week_number, $year);
$sEndDate   = date('Y-m-d', strtotime('+6 days', strtotime($sStartDate)));

?>

Reference : http://www.phpbuilder.com/board/showthread.php?t=10222903

Going from date or timestamp to get the week start and end date this call might be a better way to go.

$time = strtotime("2011-01-01");
//or
$time = strtotime("2010-12-31");

$first_day_of_week = date('Y-m-d', strtotime('Last Monday', $time));
$last_day_of_week = date('Y-m-d', strtotime('Next Sunday', $time));

//Will give the same result
//2010-12-27
//2011-01-02
?>

/**
 * Get week and its start and ending date
 *
 * @param unknown_type $date
 */
function week_start_end_by_date($date, $format = 'Ymd') {
   
   
//Is $date timestamp or date?
   
if (is_numeric($date) AND strlen($date) == 10) {
       
$time = $date;
    }else{
       
$time = strtotime($date);
    }
   
   
$week['week'] = date('W', $time);
   
$week['year'] = date('o', $time);
   
$week['year_week']           = date('oW', $time);
   
$first_day_of_week_timestamp = strtotime($week['year']."W".str_pad($week['week'],2,"0",STR_PAD_LEFT));
   
$week['first_day_of_week']   = date($format, $first_day_of_week_timestamp);
   
$week['first_day_of_week_timestamp'] = $first_day_of_week_timestamp;
   
$last_day_of_week_timestamp = strtotime($week['first_day_of_week']. " +6 days");
   
$week['last_day_of_week']   = date($format, $last_day_of_week_timestamp);
   
$week['last_day_of_week_timestamp']  = $last_day_of_week_timestamp;
   
    return
$week;
}
?>

How can I get last week start and end date in php?

date("m/d/Y", strtotime("last week monday")); date("m/d/Y", strtotime("last week sunday")); It will give the date of Last week's Monday and Sunday.

How do I get the start of the week in php?

Use strtotime() function to get the first day of week using PHP. This function returns the default time variable timestamp and then use date() function to convert timestamp date into understandable date. strtotime() Function: The strtotime() function returns the result in timestamp by parsing the time string.

How can I get start date and end date in php?

php function days($date1, $date2) { $date1 = strtotime($date1); $date2 = strtotime($date2); return ($date2 - $date1) / (24 * 60 * 60); } $date1 = '20100820'; $date2 = '20100930'; echo days($date1, $date2); ?>

How do I get the month start and end in php?

php echo 'First Date = ' . date('Y-m-01') . '
'; echo 'Last Date = ' .
date('Y-m-t') .