adding a set time to any date in php - 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'

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.

How to make my one starting and ending date-time format in php

I want to get Business start Date&Time and Business End Date&Time, which starts from 7:00 am to 7:00 am.
what I want to do is to make a query for mysql to select data between Start and End date.
For example:
$start_date = 2015-27-11 7:00:am;
$end_date = 2015-28-11 7:00:am;
between these time should be count one business day.
Select * from orders where time > $start_date AND time < $end_date;
How to make time and Date for it? please help.
Thanks in Advance!
use mysql DATE() :
SELECT * WHERE DATE(time) BETWEEN ':start' AND ':end';
If I got your question (Which you have to reformulate), Why don't you save the time and date in one field of type: Datetime?! This way, you will be able to make your comparison query easily.
Select * from orders where time>$start_date_and_time AND time < $end_date_and_time

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)

Getting a Date Range in MySQL form a UNIX Timestamp in PHP

I have a UNIX timestamp in a table like 1321521158 and want to get the details from a table for the particular date, like:
$mydate="11/11/2011";
SELECT notes FROM table WHERE `addeddate`=$mydate
How do I get it?
you can use the mysql date function like:
WHERE date(addedate) = date(mydate)
or you can calculate the starting and ending timestamp for the given day you want to know and then do
WHERE addeddate >= mystarttimestamp AND addeddate <= myendtimestamp
try
WHERE $mydate = FROM_UNIXTIME('dateColumn')'%Y/%D/%M');
MySQL Date Time Functions

Sorting through a Database query for all rows created in the same day Unix Timestamp in PHP

I am developing a PHP application which will handling many company articles.
Now I am creating a page which should order articles BY DATE ie all articles created in a certain day are shown with the appropriate heading and so on for all the articles.
I have used Unix timestamps to save the dates in the MySql db but I cant find code which only sorts dates by days. Can I please get some assistance.
Thanx
You can use mktime() to get the timestamps you would want to use to bin the entries:
http://us.php.net/manual/en/function.mktime.php
$timestamp_for_my_birthday = mktime(0,0,0,10,16,1984);
$timestamp_for_the_next_day = mktime(0,0,0,10,17,1984);
if($time > $timestamp_for_my_birthday && $time < $timestamp_for_the_next_day){
// Time is on my birthday
}
To make this into a MySQL query:
$SQL = "SELECT * FROM dates WHERE date > $timestamp_for_my_birthday AND date < $timestamp_for_the_next_day;";
To order by date I think it would go something like:
SELECT * FROM dates ORDER BY FROM_UNIXTIME(time, '%M %d %Y');
Can you do a
SELECT DATE(FROM_UNIXTIME(my_field_name)) FROM table_x ORDER BY my_field_name
You can order by the timestamp column, but if you want a string representation of which day the record belongs to, you can get any part of the date with the FROM_UNIXTIME(timestamp, 'format') function.
select FROM_UNIXTIME(timestampColumn, '%Y %m %d')
The format values in the second parameter can be adjusted based on the table here:
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format

Categories