I'm having trouble inserting a record with a date field. No matter what I do, the date field gets stored as '0000-00-00'. The value comes into the php script as a string in this format:
'9/13/2013'. I'm converting to a date using strtotime, then trying to insert the record. Here's the code. Why can't I get this to work? Thanks!
$my_date = strtotime($_POST['d']);
//die(date("Y-m-d", $my_date)); //<--outputs just fine if I uncomment this
$sql = "INSERT INTO locations (id, my_date) VALUES ('$id', '$my_date')";
$result = mysql_query($sql);
I'm converting to a date using strtotime
strtotime returns a unix timestamp (integer), not a date string. You can format it for MySQL before inserting with the date function:
$my_date = date('Y-m-d H:i:s', strtotime($_POST['d']));
Alternatively, you can use the DateTime class for this purpose.
Related
I am trying to update the field SEEN in a SQL table to the current time and date.
Here is the code:
$now=date('d-m-Y H:i:s',time());
$query="UPDATE mytable SET SEEN = '".$now."' WHERE ID_ITEM = ".$id_material;
$stmt=$dbh1->prepare($query);
$query ;
$stmt->execute();
It sets my SEEN field to 0000-00-00 00:00:00.
If I write a specific date directly into the query, say "2021-03-10 02:30:00" it would write that date into the SEEN field. But instead of $now, it would output 0s. And $now is fine, it outputs the correct timestamp.
The format you are using is not the same that MySQL DATETIME uses:
$now = (new DateTime("now"))->format("Y-m-d H:i:s");
I used this and then insert into my mysql table . but what I got is current date , I do not need current date , I need user to pick date which should be saved in table.
$pick_date = date('Y-m-d H:i:s')];
Your date format must be Y-m-d H:i:s or column type is date or datetime
insert into table (date) values ('2015-10-10') //column type date
or
insert into table (datetime) values ('2015-10-10 10:15') //column type datetime
MySQL displays the DATE type as 'YYYY-MM-DD', so you could do something like:
Suppose you are getting date from date picker as 05/11/2015. So you will first have to convert it into MySQL format as,
$value = 05/11/2015
$mysql_date = date('Y-m-d', strtotime(str_replace('/','-', $value)));
Then you can use that value in query as
insert into table_name (booking_date) values ('$mysql_date')
$query = mysql_query ('insert into pm timestamp values("'.strtotime("now").'")')
echo "<td>".date('d-M-Y H:i:s', $date3)."</td>
timestamp in database is INT(11)
I am able to get today date but I can't get the current time.
please help thank you !
You have to get the timestamp directly in your database with MySql and not with PHP. Change the type of your "timestamp" to timestamp and use the appropriate function "NOW()"
Your request would be like that : INSERT INTO table (timestamp) VALUES 'NOW()'
time();
Gives you current date and time in unix format - timestamp
$query = mysql_query ('insert into pm timestamp values("'.time().'")')
and after that when you retreive field value to var $date3 just echo it like you did
echo "<td>".date('d-M-Y H:i:s', $date3)."</td>";
If you want timestamp, use time()
php > echo time();
1406880195
I have website where the user can show his last visit to the website every time he logs in.
the type of last_activity column in the database is 'time'.
I made a code that shows the current date and save it in a variable $currentDate
and then set variable $currentDate into last_activity column in the database and update the column every time the user logs in.
but when I test the code I get this result:
your last visit: 00:00:00
the type of last_activity column in the database is 'time'.
here is my code:
<?php
session_start();
include('db.php');
date_default_timezone_set('Asia/Riyadh');
$currentDate = date('m/d/Y h:i:s a', time());
if(isset($_SESSION['Email']) === true)
{
mysql_query("UPDATE `table` SET `lastactivity` = ".$currentDate." WHERE email = '".$_SESSION['Email']."'");
$query = "SELECT * FROM `table` WHERE email = '".$_SESSION['Email']."'";
$run = mysql_query($query);
while($row = mysql_fetch_array($run))
{
$name = $row[1];
$active = $row[10];
echo 'welcome '.$name;
echo 'your last visit '.$active;
;
You should use the type DATETIME or TIMESTAMP to store a datetime value instead of TIME. The TIME datatype has no notion of dates:
The TIME Type
MySQL retrieves and displays TIME values in 'HH:MM:SS' format (or
'HHH:MM:SS' format for large hours values). TIME values may range from
'-838:59:59' to '838:59:59'.
...
Invalid TIME values are converted to '00:00:00'.
Because of that you get
your last visit: 00:00:00
back from the database as your output.
The DATE, DATETIME, and TIMESTAMP Types
The DATETIME type is used for values that contain both date and time
parts. MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD
HH:MM:SS' format. The supported range is '1000-01-01 00:00:00' to
'9999-12-31 23:59:59'.
Edit:
TIMESTAMP and in newer MySQL versions DATETIME columns have nice features, i.e. automatic update:
Automatic Initialization and Updating for TIMESTAMP and DATETIME
As of MySQL 5.6.5, TIMESTAMP and DATETIME columns can be automatically
initializated and updated to the current date and time (that is, the
current timestamp). Before 5.6.5, this is true only for TIMESTAMP, and
for at most one TIMESTAMP column per table.
Edit 2:
Furthermore produces
$currentDate = date('m/d/Y h:i:s a', time());
no valid format of an DATETIME literal. You could use STR_TO_DATE() to convert your value to DATETIME, but I wouldn't recommend this. Better you change your UPDATE statement using the MySQL function NOW() to:
mysql_query("UPDATE `table` SET `lastactivity` = NOW() WHERE email = '".$_SESSION['Email']."'");
You can format your DATETIME value, while retrieving it from MySQL with DATE_FORMAT() and give this computed column a name:
$query = "SELECT *, DATE_FORMAT(lastactivity, '%m/%d/%Y %h:%i%s %p') as last_activity FROM `table` WHERE email = '".$_SESSION['Email']."'";
$run = mysql_query($query);
while($row = mysql_fetch_array($run))
{
$name = $row[1];
$active = $row["last_activity"]; // access to the column by column name
// ...
Note
I recommend to switch from the deprecated mysql_* functions to PDO or mysqli with prepared statements and placeholders instead of concatenation of an sql statement.
I can't believe I can't do this, but I want to be able to store the current date and time from php in to a mysql table.
The column in the table is type datetime.
I've tried this
$current_date = date("Y-m-d");
$my_date = strtotime($current_date);
INSERT INTO my_table (date_time) VALUES ('$my_date')
but my timestamp comes up as 0000-00-00 00:00:00
This must be so easy to do but I just can't get it working!
I want to use the timestamp from php rather than using the mysql now() function
Try this:
$my_date = date("Y-m-d H:i:s");
INSERT INTO my_table (date_time) VALUES ('$my_date');
In the date-format parameter of the date function, use :
'H' for 24hr format
'h' for 12hr format
Don't save it as the Unix Timestamp (which strtotime() outputs), but as "2012-12-02 13:00" into the DATETIME column.
Create column type TIMESTAMP and set it to NOT NULL. Then pass in NULL during INSERT and MySQL will insert current date and time. This works for me.
set the 'type' of column named 'date_time' as 'DATETIME' and run the following query:
INSERT INTO my_table (`date_time`) VALUES (CURRENT_TIMESTAMP)
If you have the date in PHP as a timestamp, you can use the FROM_UNIXTIME function [1]
mysql> insert into table_name values (FROM_UNIXTIME(your_timestamp_here));
Hope it helped
[1]. https://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_from-unixtime
Remove the strtotime()
$current_date = date("Y-m-d");
INSERT INTO my_table (date_time) VALUES ('$current_date')
If you want to include the hour, minutes and seconds,
$current_date = date("Y-m-d H:i:s");