Given a date MM-dd-yyyy
format, can someone help me get the first day of the week?
views:
576answers:
5
A:
Try this:
function week_start_date($wk_num, $yr, $first = 1, $format = 'F d, Y')
{
$wk_ts = strtotime('+' . $wk_num . ' weeks', strtotime($yr . '0101'));
$mon_ts = strtotime('-' . date('w', $wk_ts) + $first . ' days', $wk_ts);
return date($format, $mon_ts);
}
$sStartDate = week_start_date($week_number, $year);
$sEndDate = date('F d, Y', strtotime('+6 days', strtotime($sStartDate)));
(from this forum thread)
James Skidmore
2009-12-13 21:04:31
how do I get the week number and year in all that? I have a string date in MM-dd-yyyy format
Iggy Ma
2009-12-13 21:08:39
A:
You parse the date using strptime()
and use date()
on the result:
date('N', strptime('%m-%d-%g', $dateString));
soulmerge
2009-12-13 21:11:28
+4
A:
$givenday = date("w", mktime(0, 0, 0, MM, dd, yyyy));
This gives you the day of the week of the given date itself where 0
= Sunday and 6
= Saturday. From there you can simply calculate backwards to the day you want.
Yuvalik
2009-12-13 21:21:16
A:
<?php
/* PHP 5.3.0 */
date_default_timezone_set('America/Denver'); //Set apprpriate timezone
$start_date = strtotime('2009-12-15'); //Set start date
//Today's date if $start_date is a Sunday, otherwise date of previous Sunday
$today_or_previous_sunday = mktime(0, 0, 0, date('m', $start_date), date('d', $start_date), date('Y', $start_date)) - ((date("w", $start_date) ==0) ? 0 : (86400 * date("w", $start_date)));
//prints 12-13-2009 (month-day-year)
echo date('m-d-Y', $today_or_previous_sunday);
?>
(Note: MM, dd and yyyy in the Question are not standard php date format syntax - I can't be sure what is meant, so I set the $start_date with ISO year-month-day)
micahwittman
2009-12-13 21:36:28