Hello I want to update MySQL table automatic at set time like i want to update table at 5:30 i have some values to update in my database at set time automatic. in PHP
$date = date('H:i:s');
if($date == "11:26:00") // 17:30:00 is equal to 5:30 but in 24 system
{
$sql1 = "SELECT * FROM No where id='1'";
$result1 = mysqli_query($mysqli,$sql1) or die(mysqli_error());
while($row= mysqli_fetch_assoc($result1))
{
$Foo= $row['foo'];
}
$sql2 = "UPDATE No SET id='2',foo='$foo' where id='2'";
$result2 = mysqli_query($mysqli,$sql2) or die(mysqli_error());
}
Just lie what Ashish said you can use CRON , and if you want you maybe can use this , i did not use before , try it . This code will works once ,so you have to find away to make loop cause the time will change every sec .
Note : Check the second code .
<?php
// Database connect
$date = date('H:i:s'); // Set the time to hours and minutes and seconds format
// you can echo $date to check how it looks but it will be something like this : 07:21:48
if($date == "17:30:00") // 17:30:00 is equal to 5:30 but in 24 system
{
// You can type your code here .
}
?>
Second Code :
<?php
// Remember to connect to your database
$date1 = date_create("00:00");
$date2 = date_create("23:59");
while($date1<=$date2)
{
date_add($date1,date_interval_create_from_date_string("1 sec"));
$a = date_format($date1,"H-i-s");
if($a =='17:30:00')
{
$sql1 = "SELECT * FROM No where id='1'";
$result1 = mysqli_query($mysqli,$sql1) or die(mysqli_error());
$row= mysqli_fetch_assoc($result1)
{
$Foo= $row['foo'];
$sql2 = "UPDATE No SET id='2',foo='$foo' where id='2'";
$result2 = mysqli_query($mysqli,$sql2) or die(mysqli_error());
}
die;
}
else
{
// you can type anything here , you can end the process by typing die; or anything you want
}
}
?>
You can check this and see how you can loop throw date in php :
I have 2 dates in PHP, how can I run a foreach loop to go through all of those days?
This can help you understand the time and date system and how to change the format for the date in php : How to get the current date and time in PHP?
Related
For my internship i'm almost done with a program though i need to make it so i can update my database. The problem here is that i can not update it using the date selected by the user due to the timestamp in the database. is there any way i can either add a time behind my variable or make it so i can have my update query ignore the time part?
Here is the current code for my program's update query.
if(isset($_POST['Pasaan']))
{
$Voor = $_POST['Voor0900'];
$Na = $_POST['Na0900'];
$Datum = $_POST['Datum'];
$Datum = DateTime::createFromFormat("d/m/Y" , $Datum);
$Datum->format('Y-m-d');
$Datum
$sql = "UPDATE `firsthousing` SET
`Voor0900` = '$Voor',
`Na0900` = '$Na'
WHERE Datum = '$Datum'";
if ($db->query($sql) === TRUE)
{
echo "New record created successfully";
} else
{
echo "Error: " . $sql . "<br>" . $db->error;
}
}
With kind regards,
Daynie
you can use DATE() which will only return date part
$sql = "UPDATE `firsthousing` SET
`Voor0900` = '$Voor',
`Na0900` = '$Na'
WHERE DATE(Datum) = '$Datum'";
I'm trying to update multiple cells in a MySQL database based on a previous update date. I am having an issue with unchanged, trailing cells updating to 0.
Database table looks like this (default = 0 for Day#; N = last update date):
id--Run--Day1--Day2--Day3--Day4
1----N------1-----2-----3----4
The code below does the following:
Retrieves the database entry under column= Run, id=1
Subtracts that date from today's date.
Uses that difference to move database for Day1->Day4 to the left.
Example database table when the difference is 2 days:
id--Run--Day1--Day2--Day3--Day4
1----N------3-----4-----3----4
My issue is that I need it to change all trailing Days to 0. So in this example, Day3 & Day4 should both be 0.
I've been trying out another foreach() statement within but can't get the logic behind it. Would someone please point me in the right direction?
$DaysColumnRange2 = range (1, 4);
foreach ($DaysColumnRange2 as $DaysColumnRangeLoop2){
$SubtractedDaysColumns2 = $DaysColumnRangeLoop2 - $diff2format;
$MoveToNewDay2 = ${$Day.$SubtractedDaysColumns2};
$OriginalOldDay = $diff2format + $SubtractedDaysColumns2;
$sql2 = "UPDATE users SET Day$MoveToNewDay2='$OriginalOldDay' WHERE id='$id'";
if ($conn->query($sql2) === TRUE) {
echo "RECORDS UPDATED SUCCESFULLY";
} else {
echo "Error updating record: " . $conn->error;
}
}
I'll update this first post with my attempts as I continue to work on it.
(this is not a great way of doing it, but this is what I was able to put together since Cron jobs wasn't reliable & I haven't yet figured out how MySQL Triggers work)
**
------------------UPDATE--------------
**
This section is to clarify my question.
Let's say this is my database right now:
id--Run--Day1--Day2--Day3--Day4
1----N------1-----2-----3----4
I run the code below where $diff2format = 2:
//>Database credentials + login here
//Retrieve database entry for Run
$id = 1;
$todaysdateupdate = date("Y-m-d");
$lastupdatequeryresult = mysql_query("SELECT Run FROM users WHERE id='$id'");
$lastupdaterow = mysql_fetch_assoc($lastupdatequeryresult);
//Compare Run date to today's date.
$date3=date_create($lastupdaterow['Run']);
$date4 = date_create(date("Y-m-d"));
$diff2=date_diff($date3,$date4);
$diff2format = $diff2->format("%a");
//Day1, 2, 3, etc...
$result1 = mysql_query("SELECT Day1 FROM users WHERE id='$id'");
$row1 = mysql_fetch_assoc($result1);
//Hard coded "Day#" variables
$Day = "Day";
$Day1 = $row1['Day1'];
$Day2 = $row2['Day2'];
$Day3 = $row3['Day3'];
$Day4 = $row4['Day4']; //etc
//MY QUESTION STARTS HERE****************
$DaysColumnRange2 = range (1, 4);
foreach ($DaysColumnRange2 as $DaysColumnRangeLoop2){
$SubtractedDaysColumns2 = $DaysColumnRangeLoop2 - $diff2format;
$MoveToNewDay2 = ${$Day.$SubtractedDaysColumns2};
$OriginalOldDay = $diff2format + $SubtractedDaysColumns2;
$sql2 = "UPDATE users SET Day$MoveToNewDay2='$OriginalOldDay' WHERE id='$id'";
if ($conn->query($sql2) === TRUE) {
echo "RECORDS UPDATED SUCCESFULLY";
} else {
echo "Error updating record: " . $conn->error;
}
}
The output for the database is below. Basically it copied Day3 moved it 2 times to the left, then copied Day4 and also moved 2 times to the left. Nothing changed with Day3 or Day4.
id--Run--Day1--Day2--Day3--Day4
1----N------3-----4-----3----4
But I need it to output this instead:
id--Run--Day1--Day2--Day3--Day4
1----N------3-----4-----0----0
I am working on a website which reserves lot for clients. Lot reserved has expiration date. In localhost, everything works fine. The system date and time is what my website is getting. But when I tested it online (I uploaded my website) it's not getting the system date and time.
$d = date('d M Y h:i A'); //TODAYS DATE---> this doesnt change when I change my PC's date and time
$curdate = strtotime($d)*1000;
echo $d;
$sql = "SELECT * FROM reservation";
$res = $conn->query($sql);
foreach ($res as $row) {
$exp = strtotime($row['expiration'])*1000;
$lot = $row['lot'];
$sqli = "SELECT * FROM reservation WHERE ".$curdate." >= ".$exp." AND status='reserved'";
$resu = $conn->query($sqli);
foreach ($resu as $ro) {
$id = $ro['reserve_id'];
$sl = "DELETE FROM reservation WHERE reserve_id='$id'";
$conn->query($sl);
$l = substr($ro['lot'], 1);
echo "<script>$(document).find('#_".$l."').css({'fill':'#848484'});</script>";
}
}
PHP uses the time for the machine it's running on "Server", still you can get the time from the client machine using javascript/jquery -ajax call like :
var TimeNow = new Date();
$.ajax({
url : url,
data : {date : TimeNow}
});
I have this on my login page:
$current_time = date("g:i A");
$current_date = date("l, F jS, Y");
mysql_query("UPDATE employees
SET last_login = '$current_time', last_login_date = '$current_date' WHERE username='$username' AND password='$password'");
And then on the page when the user is logged in (the protected page) I have this:
[...]
<?php
$last_login_date = $info['last_login_date']; // get last_login_date from mysql table
$last_login_time = $info['last_login_time']; // get last_login_time from mysql table
?>
[...]
You last signed in on <?php echo $last_login_date; ?> at <?php echo $last_login_time; ?>.
But this doesn't work properly because it shows the time they just logged in. How can I make it show the last time they logged in?
I also even tried adding a prev_login_time and prev_login_date to the table too, but I don't know how to make it update properly because it would update every time they login making it show the time they just logged in. (so it would be showing the current time to them)
Thanks.
Edit: [for EdoDodo]
Here's the login if statement that is setting the last IP address session variable:
if($_POST['submit']) {
$sql = "SELECT * FROM employees WHERE username='$username' and password='$password'";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
$info = mysql_fetch_array($result);
if($count == 1) {
$_SESSION['loggedin'] = 1;
$_SESSION['user'] = $username;
$_SESSION['last_login_time'] = $info['last_login'];
$_SESSION['last_login_date'] = $info['last_login_date'];
$_SESSION['last_ip'] = $info['last_login_ip'];
$return = $_SESSION['returnurl'];
if(!$return) {
$return = "/employee/";
}
date_default_timezone_set('America/New_York');
$current_time = date("g:i A");
$current_date = date("l, F jS, Y");
$ip = $_SERVER['REMOTE_ADDR'];
mysql_query("UPDATE employees
SET last_login = '$current_time', last_login_date = '$current_date', last_login_ip = '$ip' WHERE username='$username' AND password='$password'");
header("Location: $return");
exit();
}
The last login date and time is working, which is why I don't know why it isn't working with the IP one. I am accessing it correctly on the protected page:
$last_login_date = $_SESSION['last_login_date'];
if($last_login_date = date("l, F jS, Y")) {
$last_login_date = "Today";
}
$last_login_time = $_SESSION['last_login_time'];
if($_SESSION['last_ip'] != $_SERVER['REMOTE_ADDR']) {
$last_login_ip = "this IP address (".$_SERVER['REMOTE_ADDR'].")";
}
else {
$last_login_ip = "IP address ".$_SESSION['last_ip'];
}
echo "Last account login: ".$last_login_date." at " . $last_login_time . " from " . $last_login_ip . ".";
Am I doing something wrong? Maybe I'm not setting the session variable or retrieving it properly? But it seems like I am. I hope you can help. Thanks in advance.
You'll want to fetch the login time from the table in the database into the $info array before you update the database and overwrite the time with the new one. Then, you'll have the previous login time in the $info array (and, if you want to carry it across lots of different page loads, you could put it in a session variable).
I'm comparing date specified in PHP with dates in MySQL table and then echoing all rows with dates after the date in PHP. It works OK when I specify the date in the SELECT command, but when I make it a variable it doesn't work.
My PHP is:
$sql = "SELECT event FROM LifeEvents WHERE event_date > '1995-02-27'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["event"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
This works ok. But when I replace the SELECT with:
SELECT event FROM LifeEvents WHERE event_date > $date
It no longer works.
For the variable I've tried working with both:
$date = new DateTime('1995-02-27')
and
$date = '1995-02-27'
Neither of them works. The data type in MySQL is DATE.
For the string variable
$date = '1995-02-27'
$sql = "SELECT event FROM LifeEvents WHERE event_date > '$date'";
// this should work exactly as
$sql = "SELECT event FROM LifeEvents WHERE event_date > '1995-02-27'";
For the DateTime object, this should work
$date = new DateTime('1995-02-27')
$sql = "SELECT event FROM LifeEvents WHERE event_date > '" . $date->format(Y-m-d). "'";
I suggest you always set the timezone when you are dealing with dates and times because underestimate timezones can result in really bad bugs specially if your applications is dealing with many timezones and you have timestamp data type in your database.
make it a habit to do this
define('DEFAULT_TIMEZONE','UTC');//set this in your global config.php file
$date = new DateTime('2000-01-01', new DateTimeZone(DEFAULT_TIMEZONE));
Lets create a variable that holds a date and then convert it to date format that MySQL can understand.
$dt = date('Y-m-d', strtotime('1995-02-27')); // maybe filled dynamically
$sql = "SELECT event FROM LifeEvents WHERE event_date > '$dt'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["event"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
Then change what you want in order to serve your needs.
Hope I pushed you further.
That's because '1995-02-27' and DateTime('1995-02-27') are both not valid in that case :
'1995-02-27' would work if you'd have put extra quotes around $date in your SQL query : "SELECT event FROM LifeEvents WHERE event_date > '$date'"
DateTime('1995-02-27') doesn't have a __toString() magic method so PHP is unable to represent it as a string. You have to use the format method : $date = $date->format('Y-m-d') (and it will still miss the quotes)
One or another solution, you need to protect yourself against SQL injections using prepared statements! And you will actually no longer need any quote.
Try using the Carbon library for PHP.
Store a date with PHP:
$current_time = Carbon::now()->toDateTimeString();
Then just compare it the same way.