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
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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.