Jump to content

Archived

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

onlyican

Get Dates This week

Recommended Posts

Hey guys

This is where I normally build a function to do this, and someone says, U can use X();

So I thought I would ask first

What I want, is the script to work out the date this week
For example
The date today is 18/09/06 (Monday)
I want the script to work out the date FROM and To, Always starting on Monday and Ending on Sunday

As its monday today, The script should work out
FROM: 2006/09/18 (MySQL Date, Y-m-d)
TO: 2006/09/04

Its easy if its always monday, but on Wednesday (would be 2006/09/20)
I want the same dates to show

If there aint a function, I would prob get todays Day, work out how many days between today and Monday (working Backwards)
then the days to Sunday
Then if either one rolls over a new month, or evan year, to work it out

Anyone got better ideas?

Share this post


Link to post
Share on other sites
I would recommend something like this function. AFAIK, there's not a [i]built-in[/i] way to retrieve what you're after, but something like this will work nicely. Notice that with the initial [i]if[/i] statement, i'm determining what monday to start your count from, so you don't have to work two different directions, you can simply start from Monday and increment through Sunday:
[code]
<?php
// returns an array of all dates from Monday - Sunday
function getThisWeek() {
  if (date('w') == 0) $first = strtotime("last monday");
  else $first = strtotime("this monday");
  $day = 60 * 60 * 24; // one day
  $last = $first + ($day * 6); // add six days
  $week = array();
  while ($first <= $last) {
    $week[] = date('Y-m-d', $first);
    $first += $day; // increment by one day
  }
  return $week;
}

$week = getThisWeek();
echo "<pre>\n";
var_dump($week);
echo "</pre>\n";
?>
[/code]

that function will let you call the first and last records in the returned array to get the Monday and Sunday dates that you're after. hope that helps

Share this post


Link to post
Share on other sites
Exactly what I am on about

I did not know I could use strtotime like that ("last monday") ect
Wicked thanks

Share this post


Link to post
Share on other sites
[quote author=onlyican link=topic=108488.msg436489#msg436489 date=1158586615]
Exactly what I am on about

I did not know I could use strtotime like that ("last monday") ect
Wicked thanks
[/quote]

glad to help. to get a better feel for the incredible flexibility of what strtotim() will accept, check out [url=http://www.gnu.org/software/tar/manual/html_node/tar_109.html]the GNU date input formats[/url]. this is the guidelines that the strtotime() function follows.

Share this post


Link to post
Share on other sites
Cheers, I will look at that (well its bookmarked with another 100 links to catch up on)
I am a programmer, I dont have time, I eat one handed while working with the other hand (on the keyboard now people)

One more question
I have forgotten how to find out the Day from a Manually typed date

For example, if i type in 2006-7-28
How can the code work out what Day that was on?

Share this post


Link to post
Share on other sites
[quote author=onlyican link=topic=108488.msg436505#msg436505 date=1158587354]
I have forgotten how to find out the Day from a Manually typed date

For example, if i type in 2006-7-28
How can the code work out what Day that was on?
[/quote]

totally understand about the time. i'm in the same boat with you on this one :P ... here's a simple way to check what day it is:
[code]
<?php
$date = '2006-7-28';

// textual day of the week:
echo date('l', strtotime($date));

// numeric (0 = Sunday through 6 = Saturday)
echo date('w', strtotime($date));
?>
[/code]

good luck

Share this post


Link to post
Share on other sites
you can use the split function

lets say that you have a textbox linked to a variable called text...
You can use the split in this case like this
[code=php:0]
$date = split('-', $text);
//this will make $date an array spited in 3 values
//After the split you can manipulate each value like this:2006-7-28

echo "Current Year:$date[0]";
echo "Current Month:$date[1]";
echo "Current Day:$date[2]";
[/code]

Share this post


Link to post
Share on other sites
[quote author=onlyican link=topic=108488.msg436536#msg436536 date=1158589669]
But how do I find out what DAY that is?
[/quote]

check out my last post...

Share this post


Link to post
Share on other sites

×

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.