Jump to content

I have some problem with strtotime


Go to solution Solved by Jessica,

Recommended Posts

Hello guys,

 

I have a problem with the function (strtotime)

So the function &week post the week count, for example, the week 1, week 2, week 3, week 4, week 5, week 6 and when the week 6 is over then jump back to the week 1.

Example: http://dev.prodesign.lu/week/home.php

However, he is here on week 5 and yesterday he stood on the week 0 Actually, it should in turn go 1 - 2 - 3 - 4 - 5 - 6

 

Here my code:

 

<?php

$week = (strtotime(date('Y-m-d 00:00:00',time())) / 7 * 86400) % 6;

switch($week) {

case 1:
echo "
<b>Woche 1</b><br>
<br>
Montag Woche $week<br />
Dienstag Woche $week<br />
Mittwoch Woche $week<br />
Donnerstag Woche $week<br />
Freitag Woche $week<br />
Samstag Woche $week<br />
Sonntag Woche $week<br />
";
break;

//

case 2:
echo "
<b>Woche 2</b><br>
<br>
Montag Woche $week<br />
Dienstag Woche $week<br />
Mittwoch Woche $week<br />
Donnerstag Woche $week<br />
Freitag Woche $week<br />
Samstag Woche $week<br />
Sonntag Woche $week<br />
";
break;

//


case 3:
echo "
<b>Woche 3</b><br>
<br>
Montag Woche $week<br />
Dienstag Woche $week<br />
Mittwoch Woche $week<br />
Donnerstag Woche $week<br />
Freitag Woche $week<br />
Samstag Woche $week<br />
Sonntag Woche $week<br />
";
break;

//

case 4:
echo "
<b>Woche 4</b><br>
<br>
Montag Woche $week<br />
Dienstag Woche $week<br />
Mittwoch Woche $week<br />
Donnerstag Woche $week<br />
Freitag Woche $week<br />
Samstag Woche $week<br />
Sonntag Woche $week<br />
";
break;

//

case 5:
echo "
<b>Woche 5</b><br>
<br>
Montag Woche $week<br />
Dienstag Woche $week<br />
Mittwoch Woche $week<br />
Donnerstag Woche $week<br />
Freitag Woche $week<br />
Samstag Woche $week<br />
Sonntag Woche $week<br />
";
break;

//

case 6:
echo "
<b>Woche 6</b><br>
<br>
Montag Woche $week<br />
Dienstag Woche $week<br />
Mittwoch Woche $week<br />
Donnerstag Woche $week<br />
Freitag Woche $week<br />
Samstag Woche $week<br />
Sonntag Woche $week<br />
";
break;

}

?>

 

Thx for help

 

 

Link to comment
https://forums.phpfreaks.com/topic/278433-i-have-some-problem-with-strtotime/
Share on other sites

  • Solution

A. Use Code tags. (The <> button)

 

B. This is WAY TOO MUCH CODE.

$week = (strtotime(date('Y-m-d 00:00:00',time())) / 7 * 86400) % 6;
You're creating the current timestamp, converting it to a string for midnight of this day, then converting that BACK into a timestamp. Then dividing it by 7 and multiplying it by 86400 (So you're trying to get a week's worth of seconds here, you already did some math), then getting the remainder when you divide it by 6. WHY ARE YOU DOING THIS INSANE THING?

 

You need to go read the documentation for date() because you are using it way wrong. There is a format parameter to get the current week. It will be way more accurate than your method.

 

C. You also need to read on what modulo does. Any number % 6 can NEVER produce/equal 6.

 

D. You're repeating the same thing over and over regardless of what week it is. You're doing the opposite of what programming should be - you've made way more work for yourself.

 

<?php
$week = (date('W') % 6)+1;
echo "<b>Woche {$week}</b><br>
<br>
Montag Woche {$week}<br />
Dienstag Woche {$week}<br />
Mittwoch Woche {$week}<br />
Donnerstag Woche {$week}<br />
Freitag Woche {$week}<br />
Samstag Woche {$week}<br />
Sonntag Woche {$week}<br />";
Or even:

$days = array('Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag', 'Sonntag');
$week = (date('W') % 6)+1;
echo "<b>Woche {$week}</b><br><br>";
foreach($days AS $day){
    echo "{$day} Woche {$week}<br>";
}
It depends what you intend to do next really. Edited by Jessica

A. Use Code tags. (The <> button)

 

B. This is WAY TOO MUCH CODE.

$week = (strtotime(date('Y-m-d 00:00:00',time())) / 7 * 86400) % 6;
You're creating the current timestamp, converting it to a string for midnight of this day, then converting that BACK into a timestamp. Then dividing it by 7 and multiplying it by 86400 (So you're trying to get a week's worth of seconds here, you already did some math), then getting the remainder when you divide it by 6. WHY ARE YOU DOING THIS INSANE THING?

 

You need to go read the documentation for date() because you are using it way wrong. There is a format parameter to get the current week. It will be way more accurate than your method.

 

C. You also need to read on what modulo does. Any number % 6 can NEVER produce/equal 6.

 

Do you have an example?

OMFG.

 

Do I have an example. Well for one thing all I told you do to was READ THE DOCUMENTATION. So the EXAMPLE is at php.net

 

This is why I hate when I write code for other people. You're not even willing to do the work yourself to learn what you did wrong.

OMFG.

 

Do I have an example. Well for one thing all I told you do to was READ THE DOCUMENTATION. So the EXAMPLE is at php.net

 

This is why I hate when I write code for other people. You're not even willing to do the work yourself to learn what you did wrong.

Okay, thx i look at php.net.

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.