I am trying to add 7 hours to a date entered on a HTML form through PHP.
The screenshot of the form is below.
$varOpenMT4Time = $_POST['openmt4time'];
date_add($varOpenMT4Time,date_interval_create_from_date_string("40 days"));
This is the code I currently have on the php page and the MySQL INSERT statement.
Any help would be greatly appreciated.
$sql = "INSERT INTO orders (employeeName,orderNumber,system,symbol,type,volume,price,openmt4time,openlocaltime)
VALUES ('$session_user','$_POST[orderNumber]','$_POST[system]','$_POST[symbol]','$_POST[type]','$_POST[volume]','$_POST[price]','$_POST[openmt4time]','$varOpenMT4Time ')";
It can be done using Date function
$varOpenMT4Time = $_POST['openmt4time'];// Eg:-'06/02/1991 11:30'; /// use your format
$date = new DateTime($varOpenMT4Time);
$date->modify("+7 hours");
echo $date->format("Y-m-d H:i"); // mysql friendly way, used to insert in database
$currDate='2015-07-20';
$date = new DateTime($currDate);
$date->add(new DateInterval('PT7H'));
$featureDate=$date->format("Y-m-d H:i:sO");
You can use $featureDate directly into SQL Query..
echo date('H:i:s',strtotime(date('H:i:s').'7 hours'));
use this code i hope it's working..
Related
I have in my MSSQL database a column with datatype of datetime which contains some dates in this format 2021-01-11 19:58:04.277.
This is a voting system, the idea is that the users can only vote once every 24 hours.
Every time they vote this table is updated with a new record and a new date is added with the corresponding user.
I want to display a message that says how many hours left to place the next vote.
This is the code I am trying to use:
/**
* Get Votes Time
*
*/
public function getVoteRemainingTime($account) {
date_default_timezone_get();
$currentTime = date('Y-m-d H:i:s');
$sql = "SELECT VoteDate FROM dbo.vote WHERE Account = :account ORDER BY logid DESC";
$query = $this->db->prepare($sql);
$query->execute(array(':account' => $account));
$voteDate = $query->fetch(PDO::FETCH_OBJ);
$timeLeftVote = strtotime($currentTime) - strtotime($voteDate->VoteDate);
if($timeLeftVote > 86400) {
return '<strong>Vote Available!</strong>';
} else {
return $timeLeftVote;
}
}
But it is displaying the wrong information. What I am doing wrong? I would appreciate your help.
Thanks!
you need declare format parameter of the date() like date('Y-m-d H:i:s')
date_default_timezone_get();
$currentTime = date('Y-m-d H:i:s');
$timeLeftVote = strtotime($currentTime) - strtotime('2021-01-11 19:58:04.277');
if($timeLeftVote > 86400){
echo 'Vote available';
}else{
echo $timeLeftVote;
}
Instead of SELECT VoteDate FROM dbo.vote
Can you do the calculation on the time difference at source in the database using
SELECT VoteDate, DATEDIFF(HOUR, VoteDate, GETDATE()) as HourDifference from dbo.vote
As I cannot check your database query, I only checked the rest of the code and it seems to work (as Fikri F mentioned in the comments of this post) if I replace $voteDate->VoteDate by a static date.
So please provide more information. You could output the current time and the previous vote time from the database as strings, and for both dates as well the result of strtotime, and in the end the result of the method. Then please explain, what the wrong behaviour is. By this, we can narrow down the problem either to the DB query or to the PHP code.
(I would write this as a comment, but I have not enough reputation.)
i would like to help with my code how to edit HTML input date into MySQL TIMESTEMP format cause my SQL code is invalid and data are not added to my database, Thanks for help
also i asking to check this code on bottom cause 2nd SQL deppends on 1st SQL, thanks
if (isset($_POST['pridaj_anime_submit'])) {
$sql_vloz_anime = "INSERT INTO anime (a_name, a_year, a_translated_min, a_translated_max, a_rate_min, a_rate_max, a_edit, a_condition)
VALUES ('$_POST[nazov]', '$_POST[rok]', '0', '$_POST[pocet]', '8', '10', '$_POST[preklad]', '$_POST[stav]')";
mysqli_query($connect_to_db , $sql_vloz_anime);
$sql_ziskaj_a_id_pridaneho_anime = "SELECT * FROM anime WHERE a_name = '$_POST[nazov]'";
$run_sql_ziskaj_a_id_pridaneho_anime = mysqli_query($sql_ziskaj_a_id_pridaneho_anime);
$a_id_ziskane = "";
while ($db_data = mysqli_fetch_assoc($run_sql_ziskaj_a_id_pridaneho_anime)) {
$a_id_ziskane = $db_data['a_id'];
}
$sql_vloz_anime_info = "INSERT INTO anime_info (a_id, a_img, a_start, a_stop, a_time_ep, a_akihabara)
VALUES ('$a_id_ziskane' , '$_POST[obrazok]', '$_POST[zaciatok]', '$_POST[koniec]', '$_POST[cas]', '$_POST[akihabara]')";
mysqli_query($connect_to_db , $sql_vloz_anime_info);
}
$input_date=$_POST['date'];
$date=date("Y-m-d H:i:s",strtotime($input_date));
This will convert input date into MySQL Timestamp compliant format.
Please DO NOT put POST values directly in your queries. Your website or web application will be hacked in no time through SQL Injections.
MySql datetime considered yyyy-mm-dd h:i:s format.
Your input date is like dd-mm-yyyy h:i:s and so on.
Then use convert date to yyyy-mm-dd h:i:s. Read strtotime manual.
For e.g.
$inputDate = '06-06-2017 05:21:34';
$mysqlDate = date("Y-m-d H:i:s",strtotime($inputDate));
// MySql Query.
INSERT INTO table_name SET `your_field`='$mysqlDate'
i'm saving time for first login ,now when user logs in i enter time using NOW() function, that saves time in this format (data type is DATETIME.
2015-12-24 15:47:30
Now logic is like every login is first login so i've to check if there already exists an entry for today to check that i fetch time explode it and get time like this
$logintime= mysqli_query($connection,"SELECT loggedin from employees");
$loggedin_time= mysqli_fetch_assoc($logintime);
$Date = $loggedin_time['loggedin'];
$loggedin_time_converted= explode(" ",$yourDate) ;
$ConvertedDate = $loggedin_time_converted[0];
last line returns 2015-12-24 now i've date
$today= time();
$DateToday= date("Y-m-d",$today);
$DateToday also returns me same format and same date now i need your help me to compare these dates , if they are equel i dont uopdate database if they are not i will , Pleas help me how do i compare these values
You can do the test in MySQL
$result = mysqli_query($connection, "SELECT DATE(loggedin) = CURDATE() AS logged_in_today FROM employees");
$row = mysqli_fetch_assoc($result);
if (!$row['logged_in_today']) {
// code to update database
}
Wow, you've done all the hard stuff to get the problem to the point of being a simple comparison of 2 strings. This is all you need to do to get over the finish line ...
if ($ConvertedDate !== $DateToday) {
// update the database
}
You can use Php Built In function "Date Difference."
Code Seems Like As Follow:-
$today= time();
$DateToday= date("Y-m-d",$today);
$diff = date_diff($today,$DateToday);
echo "$diff days";
This will return values something like +12 days or something else.
$usersTimezone = new DateTimeZone('America/Vancouver');
$l10nDate = new DateTime($date);
$l10nDate->setTimeZone($usersTimezone);
$msg_time = $l10nDate->format('h:i A M d',time());
Not sure where is the mistake... if i put $msg_time = date('h:i A M d',strtotime($row["date_time"])); everything is working but my server time and my country time is not the same. so I need to post data in db in my own time zone to calculate.
On top of your PHP files, add this
date_default_timezone_set('America/Vancouver');
Or in your MySQL INSERT you can add this, that is if you use current_timestamp
SET time_zone = 'America/Vancouver';
I' trying to insert a DATE into MySQL database. Date has been set as new DateTime() and formatted to "Y:m:d". A variable carries the new DateTime but when inserting it to a DATE field in MYSQL only ceros are inserted. The DATE field is not getting NULL results but just CEROS 000-00-00.
This is my code.
$bigin=date("Y-m-d");
$datetime_bigining = new DateTime($bigin);
$datetime_bigining->modify('-60 day');
$datetime_bigining->format('Y:m:d');
$insert_days= mysql_query("INSERT INTO $tocreate (date_full) VALUES".$datetime_bigining->format('Y-m-d').");") or die(mysql_error());
Your code can be simplified tremendously (and fixed):
$datetime_beginning = new DateTime('60 days ago');
$insert_days= mysql_query("INSERT INTO $tocreate (date_full)
VALUES ('".$datetime_beginning->format('Y-m-d')."');") or die(mysql_error());
You can combine your first three lines of code
Your fourth line is unnecessary and useless
You have a couple of SQL syntax errors (missing parenthesis, missing quotes)
Try $datetime_bigining->format('Y-m-d'); This is the format that MySQL expects.