PHP Timestamp and if statement - php

Im using timestamps with my sql database.
I currently am making a voting system, can anyone make a if statement where if $stamp is older than two days it will run some code? i have been trying for hours and can't seem to get it to work. Thanks!

You don't need PHP to do this for you, let the database do the work as it's usually more efficient:
SELECT * FROM votes WHERE vote_timestamp <= DATE_SUB(NOW(), INTERVAL 2 DAY);
I believe this also work with UNIX_TIMESTAMP, TIMESTAMP, DATETIME and DATE columns as MySQL will perform the conversion automatically and apply it to the result set.

Something like this if you're using a unix timestamp:
$two_days_ago = strtotime('-2 days');
$sql = 'SELECT * FROM table WHERE timestamp_column < ' . $two_days_ago;
If your column is a MySQL timestamp (as others have mentioned) it would look more like this:
$two_days_ago = date('Y-m-d H:i:s', strtotime('-2 days'));
$sql = "SELECT * FROM table WHERE timestamp_column < '" . $two_days_ago . "'";

time() will give you the current time stamp. this number is in seconds.
So, everything with a timestamp which is less than time()-(2*24*60*60) is what you look for.
b.t.w usually people are reluctant to help if you do not show what you tried (or that u even tried).

Related

How to search between two datetime in php MySQL?

I have a table called reports in MySQL(MariaDB) . There is a one(out of 5) column named logdate which is is of type datetime .columns stores the the date and time (in 24hr format) .
for ex here is sample value from that column
2021-04-10 09:35:00
I have to find all reports between a given date and time .
I get 4 variables from form data in PHP
$fromdate= $_POST['fromdate'];
$todate= $_POST['todate'];
$fromtime= $_POST['fromtime'];
$totime= $_POST['totime'];
$fromtime and $totime are just integers with value from 0-23 for hours.
For example the condition may be like get all data between 4th April 2021
from 5 o'clock To 8 April 2021 18 o'clock
i.e. From 2021-04-04 03:00:00 to 2021-04-08 18:00:00. There will be never condition on minutes and seconds .
My question is how to construct a datetime in PHP compatible with MySQL types so I can have good(efficient, there are millions of records in table ) search speed?
for ex
$select = "select * from reports where logdate between ? and ? ";
P.S: I tried saving date and time as integer as unixtime stamp. But when i convert from and to date received using strttotime() I facing time format issue due to bug in my code which so can use datetime only.
If you have any suggestion to improve efficiency of DB please suggest.Thanks
Hi this link may be of help in optimizing date comparison
MySQL SELECT WHERE datetime matches day (and not necessarily time)
This one below, will help you in formatting your strtotime() by using strptime()
https://www.php.net/manual/en/function.strptime.php
Also check your spelling or typo; you wrote "strttotime()" instead of "strtotime()" yours has an extra 't' in str"tto"time, it should be str"to"time, though without the double qoutes
Though I can't say for sure this is the most effective way but you can use hour(logdate) to compare with $fromdate and $todate
$select = "select * from reports where hour(logdate) between ? and ? ";
But it will only compare hour part. Please mention how you are getting date part to compare?
It is not a good idea to make a calculation on a field in the WHERE CLAUSE. In this case MySQL / MariaDB must calculate the value from this field to comapare it to see
if this ROW has this condition. So MySQL must read the whole table FULL TABLE SCAN and CANT use any INDEX.
A better way to do this is to store the calculation on fix site. Then MySQL calculated it only one time and can use a Index ( if there one) .
you can easy use a query like this:
$select = "SELECT * FROM reports where logdate between date(?) + INTERVAL ? HOUR AND date(?) + INTERVAL ? HOUR ";
to test see:
SELECT date('2021-04-05') + INTERVAL 16 HOUR;
result:
2021-04-05 16:00:00
Here is what is working for me after using Bernds solution .
I constructing datetime string in php
$fromstr ="$fromdate"." "."$fromtime".":00:00";
$tostr="$todate"." "."$totime".":00:00";
here is my query looks like for date of 7th April to 10th April
$ select = "SELECT * FROM reports where logdate >= '$fromstr' and logdate <= '$tostr' order by logdate";
after echoing it
"SELECT * FROM reports where logdate >= '2021-04-07 3:00:00' and logdate <= '2021-04-10 5:00:00' order by logdate";```
However I am not sure if can use index for logdate column and utilize it with above query.

SQL - Time since date

I am struggling with one thing. I'm trying to calculate how many days ago a certain date was, by using SQL. The dates I have in my database can be in two formats:
Aug 28 2014, 17:17:34 CEST
Dec 29 2015, 01:03:14 CET
Those are two examples of different dates. Notice the "CET" and "CEST".
But anyways, how would I go ahead and calculate this in a SQL query? I managed to do this in PHP but I'd like to do this in the SQL query itself (if possible). Because it would save up on a lot of memory usage. I try make my work as fast as possible. I want to only access data from users that has only logged in the past 2-3 days or so. Of course I could make a SELECT * FROM users and then run PHP to check for the dates. But is there perhaps a way to do this in SQL? Like: SELECT * FROM users WHERE [lastlogin < 2 days]
Here is my current PHP code. I'd really want to do this in SQL. By the way, my columns are currently in text. Datetime does not work with that format for some reason.
$lastlogin = $row['lastlogin'];
$lastlogin = str_replace("\xc2\xa0",' ',$lastlogin);
$Date = $lastlogin;
$Date = substr($Date, 0, strpos($Date, " CE"));
$now = date('Y-m-d');
$datetime1 = new DateTime($Date);
$datetime2 = new DateTime($now);
$interval = $datetime1->diff($datetime2);
$difference = $interval->format('%a days ago');
echo "Last login was: " . $difference;
you should alter your table to clean up the data. convert the data to two columns with the timezone info in one column and the date and time in another column.
you can split the data easily using SUBSTRING_INDEX() and convert the string to datetime at the same time.
split on the "C" of "CET" and "CEST" like this:
SELECT SUBSTRING_INDEX("Aug 28 2014, 17:17:34 CEST","C",1)
and you will see you are left with the date and time part only, albeit still in a string format. That can be changed with STR_TO_DATE and you can do both on the fly.
First add the new columns to store the data:
ALTER yourtablename ADD newdatecolumn DATETIME AFTER oldcolumnname;
ALTER yourtablename ADD newtimezonecolumn VARCHAR(4) AFTER newdatecolumn;
UPDATE yourtablename
SET newdatecolumn =
STR_TO_DATE(SUBSTRING_INDEX(olddatecolumn,"C",1), '%b %d %Y, %T')
you can then use SUBSTRING_INEX again, this time splitting on the last space in the column and grabbing the timezone for the other new column
UPDATE yourtablename
SET newtimezonecolumn = SUBSTRING_INDEX(olddatecolumn," ",-1)
then you will have data that you can work with more easily to use the suggested DATEDIFF() or other time and date functions. You can drop your old date column if you need to.
Note that yourtablename etc should be changed for actual table and column names.
You can use to_days() or datediff() functions
http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html

PHP mysqli Recent activity by date

I've tried a bunch of different ways to do this that other people have done but I can't quite get it. I have a database with a bunch of print job orders, and I have a print order TRACKER on my website. On the tracker page, I'd like to display the recent activity done by displaying all of the print jobs created in the last month.
I'm trying to do this without over complicating it also.
Here I just try to make an SQL query saying WHERE Date_Completed BETWEEN lastMonth && today. Here's one of the latest snippets that I've tried (not all of the code):
$date = new DateTime('now');
$today = $date->format('Y-m-d H:i:s');
$lastMonth = date_modify($date, "-1 month");
$result = $lastMonth->format('Y-m-d H:i:s');
SELECT * FROM other_card WHERE Date_Completed BETWEEN '$today' AND '$result' order by Date_Completed");
Any information is helpful! I know the variables are named a bit odd. Just been trying to get it to work.
Try structuring the query instead like:
SELECT * FROM other_card WHERE Date_Completed < '$today' AND Date_Completed > '$result' order by Date_Completed");
You don't need to generate the dates in PHP. MySQL can do it for you:
SELECT ...
WHERE ... Date_Completed BETWEEN now() - interval 1 month AND now()
This also takes care of some edge cases where PHP could be generating the dates at 11:59:59pm on the last day of a month, and the query doesn't actually executed until 00:00:01am next month.
Between works from smaller to bigger value, not opposite
You don't need between at all, just Date_Completed > some date will do.
On a side note, you should be using prepared statements, instead of adding value directly to query

adding a set time to any date in php

basically what I need to do is add the time 00:01 to any date that has been selected by the user to find results between the date/times, can anyone help me out?
You can get a timestamp for 00:01 on a date like so:
$timestamp = strtotime($date_from_user . ' 00:01');
You can then format that timestamp as desired using date(), though you probably don't need to do that.
SELECT * FROM table WHERE datecol BETWEEN '$date1' AND '$date2'

php sql date time comparison

I have a last_notified column on my MySQL table, data type DATETIME
I want to either only select the rows where last_notified is more than n seconds old, or compare the last_notified value from the db in php.
I have tried messing around with the date object.
$d = date('Y-m-d H:i:s'); // current date and time
$dd = date('2011-09-08 10:21:34'); // this is the format used in mysql.
I know I cannot just compare them, and i'm unaware of how to add time to the date object. I have seen examples of people using something along the lines of
$t = explode(" ",$dd);
date($dd, strtotime('+30 minutes', strtotime($t[1])));
but that doesn't work . I'm just not seeing it.
You can use sql like this:
SELECT * FROM your_table WHERE last_notified < DATE_SUB(NOW(), INTERVAL n SECOND)
Take a look at your first snippet and date mask in first line
Y-m-d H:m:s
you use m mask twice, so it means there will be month instead of minutes.
you should use i mask to specify the minutes
SELECT * FROM your_table WHERE last_notified < DATE_SUB(NOW(), INTERVAL n SECOND)

Categories