This is a simple year validation that should check if the year is between 1900 and the current year. If the year is valid it should be displayed as the input's value.
if(!empty($year) && $year >= 1900 || !empty($year) && $year <= date('Y')){
$yearHolder = 'value="'.$year.'"';
}else{
$yearHolder = 'placeholder="Year"';
}
The problem I'm having is that the statement does not work, and passes any numbers through.
You can try this.
if(!empty($year) && $year >= 1900 && $year <= date('Y')){
$yearHolder = 'value="'.$year.'"';
}else{
$yearHolder = 'placeholder="Year"';
}
To be sure about conditions, place it separately.
if(!empty($year))
{
if ($year >= 1900 && $year <= date('Y'))
{
$yearHolder = 'value="'.$year.'"';
}else{
$yearHolder = 'placeholder="Year"';
}
} else {
echo "Year is empty";
}
You have to change your middle || OR condition to and AND condition, to get your logic, which you want. Right now in other words your condition is:
IF $year is NOT empty AND $year is either bigger than 1900 OR less than 2015
But what you want is:
IF $year is NOT empty AND $year is either bigger than 1900 AND less than 2015
So your if statement should look something like this:
if(!empty($year) && $year >= 1900 && $year <= date("Y"))
Also note, that the function call date() is at the end, which makes it only execute, if the other two pieces in the condition are TRUE. That is because PHP has short circuit evaluation.
Consolidating other suggestions, you could try something like this:
if (!empty($year) && $year >= 1900 && $year <= date('Y')) {
$yearHolder = 'value="' . $year . '"';
} else {
$yearHolder = 'placeholder="Year"';
}
This removes the redundant check for empty values, corrects your || to an &&
Try:
if(!empty($year) && $year >= 1900 && $year <= date('Y')){
No need to check if $year is empty twice and make them all ANDs.
Related
I have a PHP script that would insert an attendance of the student. I want to insert in the attendance status, if the time() is between 5AM to 7:30AM then my compareTime function should return P or Present and if time() is between 7:31 and 8:30 then the function should return L or Late. I can't seem to get the proper return value with this comparison between time. I badly need help.
Here is my code:
function compareTime($time,$grade){
if ($grade == "H1" || $grade == "H2" || $grade == "H3" || $grade == "H4" || $grade == "S1" || $grade == "S2") {
if ($time > strtotime('07:30:00') && $time < strtotime('8:30:00')){
return "L";
} else if ($time > strtotime('05:00:00') && $time <= strtotime('7:30:00')){
return "P";
}
} else {
if ($time > strtotime('07:15:00') && $time < strtotime('8:30:00')){
return "L";
} else if ($time > strtotime('05:00:00') && $time <= strtotime('7:30:00')){
return "P";
}
}
Insert Code:
function recordAttendance($conn, $sid, $glid, $scid){
$sql = mysqli_query($conn, "INSERT INTO tbl_attendance(student_id, gradeLevel_id, section_id, date, arrival_time, status) VALUES ('".$sid."', '".$glid."', '".$scid."', CURDATE(), CURTIME(), '".compareTime(time(),$glid)."')")or die(mysqli_error());
UPDATE: I have fixed it out. The reason why the function does not return the exact value is because of the timezone nothing being set. Therefore, whenever I get date('Y-m-d H:i:s') it does not match the date and time of my machine. By adding date_default_timezone_set('Asia/Singapore'); fixed my problem. Thanks everyone who helped!
For all strtotime add date also
strtotime('2017-08-14 07:30:00')
Instead of strtotime('05:00:00') use strtotime('today')+86400*5; (timestamp of 00:00 today + 5 hours)
This is the top part of a script I am using for redirection.
It connects to two txt based databases depending on time and redirects the user to a particular link.
There are two databases
'db1.txt' and 'db2.txt'
I only want to use the 'db2.txt' on Mondays.
All other day it should work normally, but on Mondays only, it should not change to urls1.txt.
How can I achieve that ?
$time = date("Hi", time());
if ($time >= 2224 && $time <= 2359)
{
$db = "db1.txt";
}
elseif ($time >= 0000 && $time <= 729)
{
$db = "db1.txt";
}
else
{
$db = "db2.txt";
}
Edit:
I am from India, and I am using server time in my script to avoid more complications. The script is designed to load db1.txt from 9:00AM to 6:00PM IST, and db2.txt from 6:01PM to 8:59AM. So using the "if (date('N') !=1 " wont work. I will need to change the time-zone as well.
I think you need to add this condition != monday like this
$time = date("Hi", time());
if (date('N') !=1 && $time >= 2224 && $time <= 2359)
{
$db = "db1.txt";
}
elseif (date('N') !=1 && $time >= 0000 && $time <= 729)
{
$db = "db1.txt";
}
else
{
$db = "db2.txt";
}
note : N ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0)
Check the argument documentation for the date function. With:
date('N');
you should be able to check if it's Monday and execute your desired code conditionally, ex.:
if(date('N') === 1){
}else{
}
I have following format 2016-06-06 TO 2016-06-12
I want to know that whether my date i.e 2016-06-11 lies in between or not. How can I do this?
You can use PHP if condition for checking date range:
$compareDate = "2016-06-06 TO 2016-06-12";
$dateArr = explode(" TO ",$compareDate);
$starting_date = $dateArr[0];
$final_date = $dateArr[1];
$date = "2016-06-11";
if($date >= $starting_date && $date <= $final_date) {
...
}
if($date >= $fome_date && $date <= $to_date)
{
echo "yes";
}
else
{
echo "no";
}
https://3v4l.org/UdIgq
i hope it will be helpful
Dates can be compared just as numbers can be
So using
date > starting_date && date < final_date
Will be just fine for an if clause. Also if you have I would recommend you do this in the database part as dbs have built in queries for such occasions.
I need to check if the day today is Saturday or Sunday. And i am trying to use simple if function for that but I don't know why it doesn't seem to work.
<?php
$tdate = date("D");
echo "Today is $tdate - ";
if ($tdate != "Sat" || $tdate != "Sun") {
echo "Weekday";
}
else {
echo "Weekend: $tdate";
}
?>
And the output I am getting is as follows:
Today is Sat - Weekday
What is exactly wrong with the if function?
You are performing a OR, which checks if one or the other statements are true. This means that while $tdate != "Sat" is false, $tdate != "Sun" is true. If you simply change your OR (||) to an AND (&&), your code will work fine.
if ($tdate != "Sat" && $tdate != "Sun") {
Its a logical error you need to fix
What you are saying is
If "today is NOT Saturday" OR "today is NOT Sunday", then its a Weekday
So yields TRUE because, one of the two conditions has satisfied (when the day is either Saturday or Sunday) and it goes into the true block and prints as weekday
The fix can be in two ways, 1st what xdazz gave OR the one below
<?php
$tdate = date("D");
echo "Today is $tdate - ";
if (!($tdate == "Sat" || $tdate == "Sun")) {
echo "Weekday";
}
else {
echo "Weekend: $tdate";
}
?>
if ($tdate != "Sat" || $tdate != "Sun")
when it's Sat, So it's not Sun and condition of if is true.use && instead.
I have a PHP calendar that generates a calendar based on the month and year. I use $_GET["year"]. So, it appears in the URL of the page. Therefore, the user can input any year. How can I verify that the year they entered is a year (like 2010) and not a random input (like 2t8e7sjj2)?
I figured out how to verify month like this:
$m = $_GET["month"];
if($m!=1 && $m!=2 && $m!=3 && $m!=4 && $m!=5 && $m!=6 && $m!=7 && $m!=8 && $m!=9 && $m!=10 && $m!=11 && $m!=12)
{
$m = date("m");
}
But I can't do this with year (since the year could be any number).
Also, is there a better way to verify the month other than above?
Thanks.
try http://php.net/manual/en/function.checkdate.php
if (checkdate($_GET['month'], 1, $_GET['year'])) ... okay
What are your restrictions on year other than it must be a positive integer number?
Regarding your month validation (and most probably your year one as well), you should be checking against a numeric range, eg
$m = ctype_digit($_GET['month']) ? (int) $_GET['month'] : 0;
if ($m >= 1 && $m <= 12) {
// month is valid
}
Yes, you can verify the month like this:
$month = intval($_GET['month']);
if ($month >= 1 && $month <= 12) {
// ...
}
You don't even need to verify the year; just clean it with intval()
http://php.net/intval
if (is_numeric($_GET['year']) && in_array($_GET['year'], range(1900,2100)))
also
if (is_numeric($_GET['month']) && in_array($_GET['month'], range(1,12)))
I'd do something like:
$m = intval($_GET['month']);
$y = intval($_GET['year']);
if($m < 1 || $m > 12 || $y < 1900 || $y > 2100) echo "bad input!";
2 things.
1st: you can use the pasre_url() method in php to ensure that there is no invalid formatting.
2nd: to match a year/month (since a year must be 4 numeric digits) they can be verified easily using regular expressions. Since I don't like to take credit for the sources I learned something ... look here to validate dates.
http://www.regular-expressions.info/dates.html
If you need I can write the .php for you, but it should be pretty easy from this point.
You can use this to check both ( month , year )
<?
// define vars
$months = array();
$years = array();
// create new array with months values
for($i=1;$i<=12;$i++)
array_push($months,$i);
// create new array with years values ex: begin with year 1930 to 2011
for($i=1930;$i<=2011;$i++)
array_push($years,$i);
// Check Values
if(in_array($_GET['m'],$months))
{ $m = $_GET['m']; }
else
{ $m = date("m"); }
if(in_array($_GET['y'],$years))
{ $y = $_GET['y']; }
else
{ $y = date("Y"); }
?>