Jump to content

Night and day, get the right night


Livijn

Recommended Posts

Hi, i wonder how i get the right night...

There are 4 nights every real day and each is 6h long.

I've tried but i can't get it work.

<?php
$day = date(z);

$time = date(G);
if ($time > "00.00" && $time < "00.59") {
$nightnow = "1";
} elseif ($time > "06.00" && $time < "11.59") {
$nightnow = "2";
} elseif ($time > "12.00" && $time < "17.59") {
$nightnow = "3";
} elseif ($time > "18.00" && $time < "23.59") {
$nightnow = "4";
}

$latestnight = "2";
$latestincome = "29";
$start = "29";
$day = $day - $start;
$d = $day * "4";
$d = $d + $nightnow;
$d = $d - $latestnight;

echo $day;
echo "<br>";
echo $nunatt;
echo "<br>";
echo $d;
?>

 

Link to comment
https://forums.phpfreaks.com/topic/36688-night-and-day-get-the-right-night/
Share on other sites

First of all, are you aware that the values for day and time may not hold your timezone values, right? That may be the reason it isn't displaying how it should according to your timezone, so you may have to reformat the day and time.

 

$nunatt was never assigned a value, thats why it isn't displaying anything (obviously).

Actually, it works. I dind't the first time but now it does.

Now i have another problem. In the login code, i try too put this but it doesn't work.

Code:

<?php
// Inloggning
if (isset($_POST['submit'])){

  $_POST = db_escape($_POST);
  
  $sql = "SELECT id FROM medlemmar 
         WHERE user='{$_POST['user']}' 
         AND pass='{$_POST['passwd']}'";
  $result = mysql_query($sql);
  $radd = mysql_fetch_array($result);
  
  // Hittades inte användarnamn och lösenord
  // skicka till formulär med felmeddelande
  if (mysql_num_rows($result) == 0) {
    header("Location: index.php?badlogin=");
    exit;
  } else {

  // Sätt sessionen med unikt index
  $_SESSION['sess_id'] = mysql_result($result, 0, 'id');
  $_SESSION['sess_user'] = $_POST['user'];

//Räknar ut antalet bortatimmar
$day = date(z);
$senastnatt = $radd['senastnatt'];

$time = date(G);
if ($time > "00.00" && $time < "00.59") {
$nunatt = "1";
} elseif ($time > "06.00" && $time < "11.59") {
$nunatt = "2";
} elseif ($time > "12.00" && $time < "17.59") {
$nunatt = "3";
} elseif ($time > "18.00" && $time < "23.59") {
$nunatt = "4";
}

$day = $day - $senastnatt;
$d = $day * "4";
$d = $d + $nunatt;
$d = $d - $senastnatt;

//Räknar ut vilken natt det är
$start = "29";
$dag = date(z);

$dag = $dag * "4";
$da = "4" - $nunatt;
$dag = $dag - $da;
$da = $start * "4";
$da = $da - "4";
$dag = $dag - $da;

if ($senastnatt != $dag) {
$ink = $radd['inkomst'];
$pengar = $ink * $d;
mysql_query("UPDATE medlemmar SET pengar = '{$pengar}', energi = energi + 10, senastnatt = '{$dag}' WHERE id = '{$_SESSION['sess_id']}'");
}
  header("Location: welcome.php");
  exit; 
}
}

// Utloggning
if (isset($_GET['logout'])){
  session_unset();
  session_destroy();
  header("Location: index.php");
  exit;
}

// Om inte inloggad visa formulär, annars logga ut-länk 
if (isset($_SESSION['sess_user'])){
  session_unset();
  session_destroy();
  header("Location: index.php");
  exit;
}
?>

I can't see the problem  ???

If I understand you correctly then your code is wrong

 

If you are trying to divide the time into 4 chunks of 6 hour each begininng at 00:00, 06:00, 12:00 & 18:00 you need to use

 

$time = date('G');
if ($time >= 0 && $time < 6) {
$nunatt = 1;
} elseif ($time >= 6 && $time < 12) {
$nunatt = 2;
} elseif ($time >= 12 && $time <  18) {
$nunatt = 3;
} elseif ($time >= 18 && $time < 24) {
$nunatt = 4;
}

 

 

or probably simpler

 

$time=date('G')
$nunatt=1;
if ($time >= 6)$nunatt++;
if ($time >= 12)$nunatt++;
if ($time >= 18)$nunatt++;

 

date('G') returns the value of the current hour as an integer (not as a decimal) - Your original code wouldn't set $nunatt when the hour component of time was 1,2,3,4,5,6 12 or 18 (8 hours every day)

 

Code:

//Räknar ut antalet bortatimmar
$day = date('z');
$senastnatt = $radd['senastnatt'];

$time = date('G');
if ($time >= 0 && $time < 6) {
$nunatt = 1;
} elseif ($time >= 6 && $time < 12) {
$nunatt = 2;
} elseif ($time >= 12 && $time <  18) {
$nunatt = 3;
} elseif ($time >= 18 && $time < 24) {
$nunatt = 4;
}


$day = $day - $senastnatt;
$d = $day * "4";
$d = $d + $nunatt;
$d = $d - $senastnatt;

//Räknar ut vilken natt det är
$start = "29";
$dag = date('z');

$dag = $dag * "4";
$da = "4" - $nunatt;
$dag = $dag - $da;
$da = $start * "4";
$da = $da - "4";
$dag = $dag - $da;

if ($senastnatt != $dag) {
$ink = $radd['inkomst'];
$pengar = $ink * $d;
mysql_query("UPDATE medlemmar SET pengar = '{$pengar}', energi = energi + 10, senastnatt = '{$dag}' WHERE id = '{$_SESSION['sess_id']}'");
}
  header("Location: welcome.php");
  exit; 
}
}

Archived

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

×
×
  • 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.