Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP date problem

Posted on 2004-10-22
6
Medium Priority
?
273 Views
Last Modified: 2006-11-17
Hi, I have a small problem with dates.
I try to display a week in a small loop.
I start from one day, and add seven times one day. When I try this at the end of october, the result is not what I expect.

Here is a small exmaple program:

      1 <?php
      2     // Week 44 (end of october) in 2004
      3
      4     $date = 1099173600;
      5
      6     // display the whole week
      7     for ($i=0; $i<7; $i++)
      8     {
      9         echo (date ('Y-m-d', $date + ($i*24*60*60)));
     10         echo ('<br>');
     11     }
     12 ?>

And the output:

2004-10-31
2004-10-31
2004-11-01
2004-11-02
2004-11-03
2004-11-04
2004-11-05

The first and the second date appear to bet the same (although their integer representation isn't!)

Any help appreciated

Barry
0
Comment
Question by:barrel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 27

Expert Comment

by:Diablo84
ID: 12381036
replace:

for ($i=0; $i<7; $i++)

with:

for ($i=1;$i<=7;$i++) {
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12381041
eg:

$date = 1099173600;

for ($i=1;$i<=7;$i++) {
 echo (date ('Y-m-d', $date + ($i*24*60*60)));
 echo ('<br>');
 }

otherwise i in the first loop is 0 so the timestamp will be $date+0 basically... or in otherwords identical to the timestamp
0
 
LVL 27

Accepted Solution

by:
Diablo84 earned 1000 total points
ID: 12381070
Actually with your original code, i cannot recreate the output you show, this however is a better way of writing this:

<?php
$date = 1099173600;

for ($i=0;$i<7;$i++) {
 echo date('Y-m-d',strtotime("+$i days",$date))."<br>\n";
}
?>
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 2

Author Comment

by:barrel
ID: 12381184
Hi,

Thanks for your _fast_ answer.
The fact that I start at zero is because I calculate the first day of the week which also needs to be displayed.

Your approach works for the specific date, but then I run into troubles when displaying a 'normal' week...

The fact that you cannot reproduce it makes me think that this is a PHP bug?

I am using version 4.3.4 by the way

Barry
0
 
LVL 2

Author Comment

by:barrel
ID: 12381394
Well... that did the trick.

No idea why simply adding it doesn't work and the "+$i days" does...:-/

Anyway, I am a happy man, I can continue... :-)
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12381424
There are various issues that come up when working with timestamps and php, a lot of the time its related to DST (Daylight Savings time) which can throw it out an hour and hence produce the wrong stamp. When adding time in blocks of days, weeks, months etc its always best to use strtotime in my opinion because then it works around this problem.

Best Wishes

|)iablo
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question