3

how is the best method create simply calendar?

i have for example in db:

id | date(unique)| text
1  | 2011-02-02  | aaa
2  | 2011-03-03  | bbb
3  | 2011-03-04  | ccc
..
10 | 2011-03-11  | dfg

for example now is 2011-03-04. i would like get from database -2 and +4 days: 02, 03, 04, 05, 06, 07, 08.

in php i make:

$now = strtotime("now"); and cut this substr(0, 10).

but how can i check -2 and +4 days. in this example this is simply, but what if it is 2011-11-30 and +4days this is new month. i would like make this automatically. it is possible?

3 Answers3

2
select * 
from table 
where `date` 
between curdate() - interval 2 day and curdate() + interval 4 day

PHP version

$past = date('Y-m-d', strtotime('-2 days'));
$future = date('Y-m-d', strtotime('+4 days'));
Nicola Cossu
  • 54,599
  • 15
  • 92
  • 98
1

Use PHPs(>=5.3) new DateTime class:

For example to add 4 days to a date you'd do:

$date = new DateTime('2011-11-30');
$date->add(new DateInterval('P4D'));
echo $date->format('Y-m-d');

To subtract four days, you'd use:

$date->sub(new DateInterval('P4D'));

Much nicer than strtotime, mktime etc.

Take advantage of the new features :-)

Flukey
  • 6,445
  • 3
  • 46
  • 71
0

Use mktime. So current date is, ie 2011-03-04:

$date = explode('-', '2011-03-04');
$d = $date[2] - 2;

do
{
    echo date('Y-m-d', mktime(0, 0, 0, $date[1], $d, $date[0]) . '<br />';
    $d++;
} while($d < ($date[2] + 4));

It should helps!! The rest is up to you ;)

  • @Flukey I don't told him how to do an entire calendar, just answered about this part ` i would like get from database -2 and +4 days: 02, 03, 04, 05, 06, 07, 08` .. and it works! –  Dec 06 '11 at 14:32
  • and what if the day is 01, and you subtract two days? you'll get -1! it's fundamentally flawed. – Flukey Dec 06 '11 at 14:35
  • @Flukey What you talking about ? Please run this code with any date with `day=1`, then you tell me if is flawed. –  Dec 06 '11 at 14:37
  • So it does. But try putting the date as 2011-04-01. Output: 2011-03-30 2011-03-31 2011-04-01 2011-04-02 2011-04-03 2011-04-04. As you can see, the code is buggy. There are perfectly good functions to achieve this (as i've posted above). The code you've posted is inefficient and ugly. – Flukey Dec 06 '11 at 15:10
  • @Flukey and is not what he wants?? `(...)but what if it is 2011-11-30 and +4days this is new month. i would like make this automatically` pass through a month to another one. I've done the code in the site and didn't tested, of course is ugly, but at least is mine ;) –  Dec 06 '11 at 15:14
  • Sorry to be an arse. But no, all he wants is the new date from the addition of 2 days. So if it's November 30. 2 days later it'll be the 2nd December. That's all he wants. He doesn't want it to echo out 6 different dates until it gets to the correct date such as in the example in my comment above. Please test your code before posting an answer as to ensure good quality responses to questions. Plus, with date manipulation there is absolutely no need to do an explode on the string AS php DATE functions deal with different formats. – Flukey Dec 06 '11 at 15:24
  • @Flukey hmm.. you're may be right, my english can trolled me, anyway my code works, I just understand wrong. And yes, I have to explode if I want to use `mktime`, as in my case. Just a question, you sugest to add an entire library just to sum or subtract some dates? such a wasting code. –  Dec 06 '11 at 15:37
  • But it doesn't work as it's buggy. php.net/DateTime is a built-in class. It's not a third-party library (if you're on 5.3>=) 'date('Y-m-d', strtotime('-2 days'));' That achieves what the OP wants in one line. Or the OOP way I've posted does it too. And efficiently. Use the tools which are provided out-of-the-box for you ;-) – Flukey Dec 06 '11 at 15:40