Check DB int field against timestamp - php

iam saving a date to my database as timestamp ( int field )
Now i have an external date in my php which i convert also into timestamp to compare it later.
I must make the check in the query, i cant select the date from the db before to compare them afterwards
What i need:
I wanna see if the external date is same date as the stored int value. the compare should only cover yyyy-mm-dd
little example.
my external date is 2011-02-23, which is only present without hours, the stored timestamp is lets say 123747382
What would a mysql query where clause look like, or what php am i missing in order to know that both stamps are the same date?
$r_date = '2011-02-23';
$stamp_day = strtotime($r_date);
WHERE addtime = '{$stamp_day}'
addtime would be the stored timestamp in the int field
Thank you

try this:
"WHERE DATE( FROM_UNIXTIME( `addtime` ) ) = '$r_date'"
or:
"WHERE DATE( FROM_UNIXTIME( `addtime` ) ) = DATE( FROM_UNIXTIME( '$stamp_day' ) )"

Related

PHP/SQL create a date stamp and insert it into database

I'm playing with wordpress $wpdb function.
First of all, I want to create a database table with a column corresponding to the time, so that I can save user actions into this table and know when they did it.
Creating a table:
$sql = "CREATE TABLE IF NOT EXISTS $table_name (
id INT NOT NULL AUTO_INCREMENT,
actiontime NOT NULL DATETIME,
PRIMARY KEY (id)
) $charset_collSate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
// execute the query
dbDelta( $sql );
Is this correct? Will it create a DATETIME field that I'm after? I would want a time stamp such that I can get the difference on minute/second intervals.
Inserting in such table:
$wpdb->insert($table_name, array(
'actiontime' => now()
));
Will this work?
I can't check it manually, because I'm developing on a side machine and only have ftp access to live-server and notepad. I can't test anything locally.
I use this php function to get the current value for a MySQL DATETIME field.
/**
* Return current date time.
*
* #return string ISO date time (Y-m-d H:i:s)
*/
function now()
{
return date('Y-m-d H:i:s');
}
There are a few options you could consider,
If you're using plain old PHP and MySQL, you can use the SQL now() function to insert data into a timestamp field like this,
INSERT INTO projects (actiontime) VALUES (now())
Another options would be to use a PHP variable such as, $timestamp = date('Y-m-d G:i:s'); and inserting it in a SQL query:
$timestamp = date('Y-m-d G:i:s');
INSERT INTO projects (actiontime) VALUES ($timestamp);
Hope it helps!
try PHP time() for epoch timestamp

How to insert the current date to database then retrieve it?

$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

PHP Insert Query - Date field 0000-00-00

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.

Insert timestamp posted value in mysql in php

I have input where I get values like:
$water = 11/04/2013 00:00:00
(day/month/year) hour:minute:second
And a table definition:
CREATE TABLE `my_to` (
id_a INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
last_time timestamp ,
water timestamp
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
but when I am trying to do an insert I get no value in the table...
Should I convert the day or something?
supposing $water has a date and time value?
$timestamp = strtotime($water);
Insert into table .... $timestamp ?
You can use MySQL's STR_TO_DATE function:
INSERT INTO `table`( `date_field` )
VALUES( STR_TO_DATE( $water, '%d/%m/%Y %H:%i:%S' );
http://dev.mysql.com/doc/refman/5.6/en/timestamp-initialization.html
Correct syntax is: 0000-00-00 00:00:00
so ..
$time = '11/04/2013 00:00:00';
$time = strtotime($time);
$time = date('Y-m-d H:i:s', $time);
# INSERT TIME...
The timestamp you want to insert must have the format YYYY-MM-DD HH:II:SS.
You can use date('Y-m-d H:i:s', strtotime($water)).
Hope this helps!
Timestamps in mysql must be in YYYY-MM-DD HH:II:SS format. You can do this in php with
Date('Y-m-d H:i:s', strtotime($water))
You could also use the FROM_UNIXTIME mysql function
water DATETIME
or fill the field with NOW() or CURRENT_TIMESTAMP
Be aware that strtotime() interprets that date in the format mm/dd/YYYY, and not as dd/mm/YYYY.
You should rearrange date in a different format (YYYY-mm-dd would be the best, imho).

Store php datetime in mysql database

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");

Categories