Jump to content


Photo

Get Dates This week


  • Please log in to reply
9 replies to this topic

#1 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 18 September 2006 - 01:15 PM

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?
Tell me the problem, I will try tell you the solution

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 18 September 2006 - 01:28 PM

I would recommend something like this function. AFAIK, there's not a built-in way to retrieve what you're after, but something like this will work nicely. Notice that with the initial if 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:
<?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";
?>

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
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 18 September 2006 - 01:36 PM

Exactly what I am on about

I did not know I could use strtotime like that ("last monday") ect
Wicked thanks
Tell me the problem, I will try tell you the solution

#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 18 September 2006 - 01:40 PM

Exactly what I am on about

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


glad to help. to get a better feel for the incredible flexibility of what strtotim() will accept, check out the GNU date input formats. this is the guidelines that the strtotime() function follows.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#5 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 18 September 2006 - 01:49 PM

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?
Tell me the problem, I will try tell you the solution

#6 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 18 September 2006 - 02:13 PM

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?


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:
<?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));
?>

good luck
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#7 LAX

LAX
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 18 September 2006 - 02:22 PM

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
$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]";


#8 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 18 September 2006 - 02:27 PM

But how do I find out what DAY that is?
Tell me the problem, I will try tell you the solution

#9 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 18 September 2006 - 04:03 PM

But how do I find out what DAY that is?


check out my last post...
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#10 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 18 September 2006 - 06:00 PM

soz, didn't see that, I only saw the post by lax RE explode
Tell me the problem, I will try tell you the solution




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users