Jump to content

[SOLVED] check start of week problem


fry2010

Recommended Posts

Can someone help me to solve this problem I cant quite get my head round?

I need to store figures on a daily basis. They can be positive and negative numbers.

I will store these in a database.

I need to find the accumulative total for the current week. So say today is wednesday, I need to total mon and tuesday. But then if the day is friday I will need to calculate mon, tue, wed and thursday... So basically I need to find the start of the week then I can do my calculations from that. I am using the mysql setting for dates in the database so YYYY-MM-DD.

I will also need to find out the total for last week, but if today is tuesday then I cant just count back 8 days to find the start it will need to be dynamic. I suppose once I find the start of the current week this might not be an issue.

Also I need to find the start of the current month and last month.

 

Anyone know what I should be using for this?

 

 

Link to comment
https://forums.phpfreaks.com/topic/147814-solved-check-start-of-week-problem/
Share on other sites

<?php
$dt = "2010-03-11";

$dayToday = date("N", strtotime($dt));

$dayDiff = $dayToday - 1;// 1 is for monday..

$dayStartOfWeek = date("Y-m-d", strtotime($dt) - $dayDiff * 86400);

echo $dayStartOfWeek;

?>

 

this will give u start day of the week (date on monday of that week), provided any date...hope this is what u r looking for.

awesome, works a treat. Thank you very much samshel.

 

Iv actually just had a look through the code and tweaked a few things to see how it returns the value etc.. I have to say it is amazing that you could just cook something like that up so quick. This kind of thing totally eludes me. Amazing stuff.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.