Jump to content

PHP Date and changing it


ababmxking

Recommended Posts

Ok so the deal is, i want to setup a ban page (for my online PvP RPG). I want to give my staff the option to ban for X amount of hours, X amount of days, X amount of weeks, X amount of months or X amount of years. The way i'm doing it is like so.

 

	$hour     = date(G);
	$day      = date(j);	
	$month    = date(m);
	$year     = date(Y);
	$week     = date(W);
	$daycount = date(t);

 

And then I add to day, month, hour, year, week, according to which option the staff member is trying to ban for.

 

My problem is, say the day is the 31st, and its the 22nd hour in the day, and they ban for 3 hours, well i have to update the day, and if the updated day is greater than the day count, then i have to update month, week, etc. So far i've worked out quite a bit of the kinks, but i still have quite a few too. And the php for this alone is already 140 lines i think.

 

Heres the full code ( its a bit messy and probably a little hard to understand)

<?php
if($_POST['submit']){
$ban = $_POST['ban'];
$amount = $_POST['amount'];

	if(strlen($ban) == 0){
		echo"Please enter a ban length";
	}

		else{
	$hour     = date(G);
	$day      = date(j);	
	$month    = date(m);
	$year     = date(Y);
	$week     = date(W);
	$daycount = date(t);
		$testing = 1;
		if($testing){
			$hour = 24;
			$day = $daycount;
			$month = 12;
			$week = 52;
			}

		$fullodate = "$month/$day/$year  $hour  $week";

		switch($amount){
			case 1:
					if($ban >= 24){
						$error = "You are trying to ban for 24 or more hours, please change the option to days";
					}

					else{
						$hour = $ban + $hour;
					}
			break;

			case 2:
					if($ban >= 7){
						$error = "You are trying to ban for 7 or more days, please change the option to weeks";
					}

					else{
						$day = $day + $ban;
					}
			break;

			case 3:
					if($ban >= 4){
						$error = "You are trying to ban for 4 or more weeks, please change the option to months";
					}

					else{
						$week = $week + $ban;
					}
			break;

			case 4:
					if($ban >= 12){
						$error = "You are trying to ban for 12 or more months, please change the option to years";
					}

					else{
						$month = $month + $ban;
					}
			break;

			case 5:
					if(strlen($ban) > 2){
						$error = "You are trying to ban for more that 99 years. There is no need to ban for longer than that. Change your length to 0 and it automatically do a permanent ban on the user.";
					}

					else{
					$year = $year + $year2;
					}
			break;
		}

		for($i=0; $i <= 5; $i++){
			if($hour > 24 && $hourset == 0){
				$day = $day + 1;
				$hour = $hour - 24;
				$hourset == 1;
			}

			if($day > $daycount && $dayset == 0){
				$month = $month + 1;
				$day = $day - $daycount;
				$dayset == 1;
			}

			if($week > 52 && $weekset == 0){
				$year++;
				$week = $week - 52;
				$weekset == 1;
			}

			if($month > 12 && $monthset == 0){
				$year = $year + 1;
				$month = $month - 12;
				$monthset == 1;
			}

			if($month == 1){
				$week = 1;

					if($day >= 7){
						$week = $day /7;
						$week = round($week);
					}

			}
					if($amount == 4){
						$week = $month * 4;
					}

						if($amount == 3){
							$day = $day + $week * 7;
								if($day > $daycount){
									$month = $month +1;
									$day = $day - $daycount;
								}
						}
		}
			if(strlen($error) == 0){
			$fullmoddate = "$month/$day/$year  $hour $week";
			}
				else{
					$fullmoddate = $error;
				}

		$return[0] = "$fullodate";
		$return[1] = "$fullmoddate";
						echo"$fullodate <br /> $fullmoddate <br />";

		echo "<br /><br /> This is a test, something should come up after this. $return[0] and $return[1]";
		}
}

?>

<html>
<head>
</head>

<body>
<script language="JavaScript">
function onlyNumbers(evt)
{
var e = event || evt; // for trans-browser compatibility
var charCode = e.which || e.keyCode;

if (charCode > 31 && (charCode < 48 || charCode > 57))
	return false;

return true;

}
</script>

<form method="POST" action="">
<input type="text" name="ban" onKeypress="return onlyNumbers();" /> - 
	<select name="amount">
		<option value="1">Hours</option>
		<option value="2">Days</option>
		<option value="3">Weeks</option>
		<option value="4">Months</option>
		<option value="5">Years</option>
	</select>	
		<br />
<input type="submit" value="Submit" name="submit" />
</form>

</body>
</html>

 

So im wondering if there is a more efficient way of doing this, or if i just need to keep on going and fix all the little bugs that come with it?

Link to comment
Share on other sites

There are other ways but this is easy to understand and learn from.

$today=date("U");
$mysql_date='2010-03-12';
list($y,$m,$d)=explode("-",$mysql_date);
$ts = mktime(0, 0, 0, $m, $d, $y);
if($today>$ts)
echo "mysql_date is past";
else
echo "mysq_date is future";

 

Using just a date(2010-03-11) and running it through mktime gives a timestamp of midnight of the date. If you need you can run $today=date("Y-m-d") the same as the mysql timestamp.

 

HTH

Teamatomic

Link to comment
Share on other sites

thanks for the help guys, but i couldnt figure out that strtotime function, so what im doing instead, and its less accurate. The could either get banned  for a little bit longer or get lucky and be banned for a little bit less. BUT they are being banned, so i honestly dont care. They can deal with it lol. They are lucky im even coding something like this for them, so we can have the option to ban them for a week instead of permanently.

 

But what im doin is, just using hours, days and years. And im multiplying, dividing or whatever i need to do, to set the days to the right ban day, and year. So say they're getting banned for 6 months, what im doing is,

 

$day = date(whatever letter it is to have 0-365 as a day);

        //for months
     $day += $banLength * 30.416;

 

so it makes it so much easier for me to figure out how to do the un-ban automatically, and makes everything simple for me.

 

If anybody wants to give some pointers feel free, im always open.

Link to comment
Share on other sites

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.