Tuesday, April 4. 2006Figuring the Start of the Week with PHPTrackbacks
Trackback specific URI for this entry
No Trackbacks
Comments
Display comments as
(Linear | Threaded)
Great stuff!
However how do you make it find the start of next week??? Cheers
I have not tried this, but you could try just adding 7 to the day calculation:
date('d')-date('w')+7
Hi Mike, thanks for getting back to me. Havin a bit of trouble with this. My code looks like this:
$weekStart = date(mktime(1,0,0,date('m'), date('d')-date('w'), date('y'))); And then I have the next days eg: $day2 = date(mktime(1,0,0,date('m'), date('d')+1-date('w'), date('y'))); I tried what you said and tried changing my +1 to 7 but the date stays on the start of the current week. Confusing! :S Cheers
This is what I tried:
date('Y-m-d', mktime(1, 0, 0, date('m'), date('d')-date('w')+14, date('Y'))) That produces 2009-05-24 as expected. Then this: date('Y-m-d', mktime(1, 0, 0, date('m'), date('d')-date('w')+7, date('Y'))) That produces 2009-05-31, the start of next week. Hope that helps.
No way, it was all my fault! I was using the start of week variable for calculation for the rest of the days. I wasn't acutally displaying, hence it didnt change.
Great to see a blog that is active. Excellent work, thanks
Hi Gin, I accidentally deleted your last comment. Anyway, I think date('H:i:s') should always give you the current time, daylight savings or not. I guess it depends on how your timing function works as to whether a daylight savings change would affect it. If it's running during the time of night your computer changes its time, it would be worth testing.
No worries mate. Did a bit of digging and it appears as though that will be effected by timezones. But thinking about it, if your using a local php server then it will change at the same time as the rest of the worlds clocks. I'll keep an eye on it. cheers
Simplest:
strtotime("last Saturday + 1 day") this returns the following: on Sunday: returns midnight on Monday: returns last Sunday's date ... on Saturday: returns 6 days ago
I'd set it up as a function.
## function weekfinder($dformat, $date) { $date_parts = explode($dformat, $date); $dtemp = date("N",mktime(0,0,0,$date_parts[0],$date_parts[1],$date_parts[2])); if($dtemp == 1){ return date("m/d/Y",mktime(0,0,0,$date_parts[0],$date_parts[1],$date_parts[2])); }elseif($dtemp != 1){ $dtsub = ($dtemp -1); return date("m/d/Y",mktime(0,0,0,$date_parts[0],$date_parts[1] - $dtsub,$date_parts[2])); } } $var = weekfinder("/", $datevar); ## This does not require any hour specifications
easier :
$data['firstDay'] = date("m/d/Y", mktime(0, 0, 0, date("m"), date("d") - date("w"), date("Y"))); $data['lastDay'] = date("m/d/Y", mktime(0, 0, 0, date("m"), date("d") - date("w") + 6, date("Y"))); |
CategoriesQuicksearchArchives |