Compare date in database table with current time - php

I'm having a challenge comparing the current date with a date set in a database table CF_PROCUREMENT with the column name TENDER_DEADLINE_PROC for verification code in a portal. The TENDER_DEADLINE_PROC field needs to be in comparison with the current time but what seems to be put into comparison is two current timestamps. I am new to PHP.
$sql = "SELECT * FROM CF_PROCUREMENT WHERE CASEFOLDERID = '$code'";
$result = sqlsrv_query($db,$sql);
if ($result) {
$rows = sqlsrv_has_rows($result);
if($rows==true){
$date = new DateTime($row['TENDER_DEADLINE_PROC']);
$date = $date ->format('d/m/Y H:i:s');
$now = new DateTime();
$now = $now ->format('d/m/Y');
if($date > $now){
$mail->Subject = 'PORTAL Deadline Alert';
$mail ->Body = '
<html>
<body>
<h1>Deadline has lapsed</h1>
</body>
</html>';
$mail->send();
}else{
$mail->Subject = 'PORTAL Email Verification';
$mail ->Body = '
<html>
<body>
<h1>VERIFICATION CODE</h1>
<h2>Code: <b>'.$otp_code.'</b>
</body>
</html>';
}

Related

How to insert result of date difference into mysql database using php?

<?php
function find_days($start_date, $end_date) {
$response = new stdClass();
try {
$sdate = new DateTime($start_date);
$edate = new DateTime($end_date);
$dateInterval = $edate->diff($sdate);
$response->status = true;
$response->result = $dateInterval;
return $response;
} catch (Exception $e) {
$response->status = false;
$response->result = 'Invalid Date Format';
return $response;
}
}
?>
Start Date: <input type="date" name="sdate" placeholder="start date" />
End Date: <input type="date" name="edate" placeholder="end date" />
<input type="submit" value="Find Days" />
<?php
if (isset($_POST['sdate']) && $_POST['sdate']) {
$start_date = $_POST['sdate'];
$end_date = $_POST['edate'];
//now call the function
$days_array = find_days($start_date, $end_date);
if ($days_array->status) {
echo " <input type='text' name='day'
value='.$days_array>result>days.' />";
$day = $_POST['day'];
$query = "INSERT into cart (date,edate,days)
VALUES('$start_date','$end_date','$day')";
$success = $conn->query($query);
if (!$success) {
die("Couldn't enter data: ".$conn->error);
}
} else {
echo $days_array->result;
}
}
My code is working perfectly. But the result is displayed only on the screen.
So I've tried to store the result by placing it in a textbox and then insert into the table in a usual way. But I got an error "Catchable fatal error: Object of class DateInterval could not be converted to string in C:\xampp\htdocs\date.php on line 45"
I don't know how to rectify this.. please help me solve this.
You have to convert it to a string using format:
<?php
$now = new DateTime();
$hour = new DateTime( "now +1hours +13minutes +22seconds" );
$diff = $now->diff( $hour );
echo $now->format('Y-m-d H:i:s');
echo "\n";
echo $hour->format('Y-m-d H:i:s');
echo "\n";
echo $diff->format('%H:%I:%S');
Output
2018-07-26 20:53:42
2018-07-26 22:07:04
01:13:22

when I use "modify" on my timestamp it gives a wrong output

I want to block a someone who tried to login 4 times with a wrong password.
The problem is: When I use "modify" and change the timestamp with 15 minutes, I get -1 as output and after two minutes I get -2 as output. I tied for so long and I searched a lot on internet, but it still doesn't work.
How I want it to work:
In the database is the column: "falselog". If the username of the visiter is correct but the password is incorrect, falselog will be +1. When falselog is 4, the visiter will be banned for 15 minutes. So the visiter is able to try 4 times. After 15 minutes, the visiter can try again.
This is my object with all the code:
public function logUser($username, $password) {
// query id = 2
$sql2_1 = "SELECT
id,
password,
falselog,
lastlogin
FROM users
WHERE username ='".$username."' ";
$result2_1 = $this->con->query($sql2_1);
$fetch2_1 = mysqli_fetch_array($result2_1);
$count2_1 = $result2_1->num_rows;
$now = new DateTime('now');
$blockedSince = new DateTime($fetch2_1['lastlogin']);
$fout = $fetch2_1['falselog'];
$date_old = date ("Y-m-d H:i:s", strtotime("now"));
$block = date("i", $fetch2_1['lastlogin']) + 16;
$current = date("i", strtotime("now"));
$wait = $block - $current ;
$dbtime = date ("Y-m-d H:i:s", strtotime($date_old));
// This is the code that doesn't work
if ($fetch2_1['falselog']>=4 AND $blockedSince->modify('+15 minutes') > $now) {
$error[] = 'This account has been banned, try again about '.$wait.' minutes';
$decline = true;
$date_old = $fetch2_1['lastlogin'];
}
elseif (!preg_match("/^[A-Za-z0-9-]+$/", $username)) {
$error[] = 'De input bevat ongeldige tekens (alleen cijfers en letters toegestaan)';
}
elseif ($count2_1 === 0) {
$error[] = 'Wrong login data';
}
elseif ($fetch2_1['password']!=sha1($password)) {
$error[] = 'wrong password';
$fout = $fetch2_1['falselog']+1;
}
if ((count($error) == 0) OR ($fetch2_1['falselog']==4 AND $blockedSince->modify('+15 minutes') < $now)) {
$fout = 0;
}
$sql2_2 = "UPDATE users SET
falselog='".$fout."',
lastlogin='".$dbtime."'
WHERE username='".$username."' ";
if ($this->con->query($sql2_2) === TRUE) {
if (count($error) == 0) {
return false;
}
else {
return $error;
}
}
else {
echo "Error: " . $sql2_2 . "<br>" . $this->con->error;
return true;
}
}
You should check that here
$blockedSince = new DateTime($fetch2_1['lastlogin']);
you get the correct data for DateTime, try var_dump($blockedSince); after this line, and check that it has correct value inside;
try this
$blockedSince = mktime($fetch2_1['lastlogin']);

cron job, birthday check, and sending email (just making sure)

I have created send_bday_email.php which send birthday email 2 weeks prior to customer's birthday.
<?php
$today = date('Y-m-d');
$todayMD = date('m-d');
include_once "../../mysqli_connect.php";
$sql= mysqli_query($dbc, "SELECT * FROM Customer_Info");
$mail_body = '';
while ($row= mysqli_fetch_array($sql)) {
$id = $row["id"];
$email = $row["email"];
$birthday = $row['birthday'];
$bday = new DateTime($birthday);
$bday->sub(new DateInterval('P14D'));
$twoWeekbday = $bday->format('m-d');
if ($todayMD==$twoWeekbday) {
if(empty($row["firstName"])){
$name = 'Customer';
} else {
$name = $row["firstName"];
}
$mail_body = file_get_contents('../forms/bday_email_form.html');
}
}
This is my part of code which has two weeks prior and if customer's two week prior is equal to today send birthday email.
If I run this code daily using cron job, will it send emails perfectly?
I wanted to make sure before I actually test it out with my customer.

Timezone conversion not working

To get timestamps to be in the user's time period, I have set up a timezone conversion tool with PHP for my posting system. Basically the user sets their own timezone through a dropdown, and this is stored on the database as a PHP timezone - so if I select GMT, the value in the database would be Europe/London.
The idea is then to take this from the database and plug it into a timezone conversion piece of code, but this presently isn't converting anything at all. Can anyone see the problem?
doMessage.php
$date = date("Y-m-d G:i:s");
$user_tz = $result['time'];
$schedule_date = new DateTime($date, new DateTimeZone($user_tz) );
$schedule_date->setTimeZone(new DateTimeZone('UTC'));
$date = $schedule_date->format('Y-m-d H:i:s');
core/init.php
<?php
session_start();
$db = new PDO('this is all correct');
if(isset($_SESSION['id'])) {
$i = $_SESSION['id'];
$query = $db->prepare("SELECT * FROM users WHERE id=:i");
$query->bindParam(":i",$i);
$query->execute();
$result = $query->fetch();
$ver = $result["activated"];
if($ver == 0) {
echo "Please <a href='activate.php'>verify your account.</a>";
}
}
else {
echo "Please <a href='login.php'>log in.</a>";
$guest = True;
}
To get current date and time of GMT, just this is enough:
date_default_timezone_set('GMT');
$current_date = date("Y-m-d h:i:s A T")
echo $current_date;
This outputs 2013-12-30 5:30:20 AM GMT

PHP mail sending when product got expire

i want to send the mail , when product got expire from before and ondate and after day, in php i was used datediff mysql function with php, but if product expire date comes like 31-1-2012 , the differ value is not suit for my coding , plz help how to solve it ,
This is my coding
<?php
$sql = mysql_query("SELECT * FROM supplier_product_det");
$results = mysql_num_rows($sql);
//echo '<script>alert("'.$results.'")</script>';
if ($results > 0)
{
for($i=0;$i<$results;$i++)
{
while($rows = mysql_fetch_array($sql))
{
/* print_r($rows['expired_on']);?><br /> <?*/
$exdate = $rows['expired_on'];
$curdate = date('Y-m-d');
$datedif = mysql_query("select datediff('".$curdate."','".$exdate."')");
while( $date = mysql_fetch_array($datedif) )
{
//echo $date['0'];
//echo $rows['pro_code'];
if (($date['0'])== 1)
{
if(($rows['mailcount'])!== $curdate)
{
$to = $rows['supplier'];
$subject = "Product Expire Intimation";
$message = "Dear Mr/Miss/Mrs
The Following Your Product Expired
Product Code:".$rows['pro_code']."
Product Name:".$rows['product_name']."
Product Expire Date:".$rows['expired_on']."";
$from = "tone#bgrow.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
$checkdate = mysql_query("update supplier_product_det set mailcount ='".$curdate."' where id='".$rows['id']."'") or die(mysql_error());
}
//echo $rows['supplier'];
}
if (($date['0'])== 0)
{
if(($rows['mailcount'])!== $curdate)
{
$to = $rows['supplier'];
$subject = "Product Expire Intimation";
$message = "Dear Mr/Miss/Mrs
The Following Your Product Expired
Product Code:".$rows['pro_code']."
Product Name:".$rows['product_name']."
Product Expire Date:".$rows['expired_on']."";
$from = "tone#bgrow.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
$checkdate = mysql_query("update supplier_product_det set mailcount ='".$curdate."' where id='".$rows['id']."'") or die(mysql_error());
}
//echo $rows['supplier'];
}
if (($date['0'])== -1)
{
if(($rows['mailcount'])!== $curdate)
{
$to = $rows['supplier'];
$subject = "Product Expire Intimation";
$message = "Dear Mr/Miss/Mrs
The Following Your Product will Expire Today
Product Code:".$rows['pro_code']."
Product Name:".$rows['product_name']."
Product Expire Date:".$rows['expired_on']."";
$from = "tone#bgrow.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
$checkdate = mysql_query("update supplier_product_det set mailcount ='".$curdate."' where id='".$rows['id']."'") or die(mysql_error());
}
//echo $rows['supplier'];
}
}
}
}
}
?>
Write your second select query as follows
SELECT DATEDIFF($exdate, CURRENT_DATE) as diff;
Also, no need to write separate select query you can get same column in first query itself as
SELECT column1, column2, column3, DATEDIFF(expired_on, CURRENT_DATE) as diff
FROM supplier_product_det
You may need to change position of expired_on and CURRENT_DATE to get correct diff value
Try something like this...
PHP: $now = date('Y-m-d', strtotime('now'));
-
SQL: WHERE $now >= DATE_FORMAT(expires, "%Y-%m-%d")

Categories