Search between two dates in mysql and php - php

Hi I want to search the items present in the table users. The code I wrote is
$fromdate = date("Y-m-d H:i:s", strtotime($_POST['fromdate']));
$todate = date("Y-m-d H:i:s", strtotime($_POST['todate']));
$s="SELECT * FROM logs WHERE dt BETWEEN $fromdate AND $todate ORDER BY dt DESC ";
$x = mysql_query($s);
But the output is not showing. Please someone help me.

Need to apply quotes around '$fromdate' and '$todate' as
$s="SELECT * FROM logs WHERE dt BETWEEN '$fromdate' AND '$todate' ORDER BY dt DESC ";

$fromdate = strtotime($_POST['fromdate']);
$todate = strtotime($_POST['todate']);
$query="SELECT * FROM logs WHERE UNIX_TIMESTAMP(dt) BETWEEN $fromdate AND $todate ORDER BY dt DESC ";

Related

Search data between two dates in dd/mm/yyyy hh:mm:ss in sql

I want to fetch all records between two dates from database in php. Date format is dd/mm/yyyy hh:mm:ss. example 06/Dec/2016 05:56:15
I'm using following code
$Sdate=date_create($_GET['sdate']);
$start=date_format($Sdate,"d/M/Y H:i:s");
$Edate=date_create($_GET['edate']);
$end=date_format($Edate,"d/M/Y H:i:s");
$sql = "SELECT * FROM `payments` WHERE `customerid` = '".$_SESSION['id']."' AND dateandtime BETWEEN ('".$start."', '".$end."') ORDER BY id DESC";
But this is not working
Thank You in advance for helping me
Try this:
$Sdate = date('Y-m-d H:i:s', strtotime($_GET['sdate']);
$Edate = date('Y-m-d H:i:s', strtotime($_GET['edate']);
$sql = "SELECT * FROM `payments`
WHERE `customerid` = '".$_SESSION['id']."'
AND dateandtime BETWEEN '$Sdate' AND '$Edate'
ORDER BY id DESC";
The correct syntax is:
dateField BETWEEN dateFieldLow AND dateFieldHigh

select date that is less or equal to current date

I am trying to select the date in the field end_date that is less or equal to current date but did not work and my field end_date as the same date format(08-09-2014) data as current date below is my code thanks
$currentdate = date("d-m-Y");
$query1 = "SELECT * FROM location WHERE end_date <= '$currentdate'";
$result1 = mysql_query ($query1) or die('query error');
while( $line1 = mysql_fetch_assoc($result1)){
echo $line1['end_date'];
}
try this
SELECT * FROM location WHERE end_date <= DATE_FORMAT(CURDATE(), '%Y-%m-%d')
try with -
SELECT * FROM location WHERE end_date <= DATE_FORMAT(CURDATE(), '%d-%m-%Y')
$currentdate = date("Y-m-d");
$query1 = "SELECT * FROM location WHERE `end_date` <= '$currentdate'";
$result1 = mysql_query ($query1) or die('query error');
while( $line1 = mysql_fetch_assoc($result1)){
echo $line1['end_date'];
}
try this... must have in database date format;;
If your currentdate is independent of the system date (e.g. if you are operating over different timezones), and if, for some reason, your end_date is not a date type, then try this:
SELECT * FROM location
WHERE str_to_date(end_date, '%d-%m-%Y') <= str_to_date('$currentdate', '%d-%m-%Y')
where the format can be changed to match your inputs.
If you want to compare dates, then make sure you are comparing dates and not strings.

How to group records by date when date is in UNIX Timestamp format?

I'm getting two dates from PHP form in the dd-mm-yyyy format.
(Say 01/06/2013 and 30/06/2013)
Now I'm using following code to display the datewise result among the date range as given above, but it's not working for me as the dates stored in DB are in UNIX Timestamp format(transaction_date bigint(12)). How should I display the datewise results then? Can anyone help me in resolving this issue?
if($form_data['from_date']!='' && $form_data['to_date']!='') {
$from_time = explode("/", $form_data['from_date']);
$to_time = explode("/", $form_data['to_date']);
$start_date = mktime( 0,0,0,$from_time[1],$from_time[0],$from_time[2] ) ;
$end_date = mktime( 23,59,59,$to_time[1],$to_time[0],$to_time[2] ) ;
$sql =" SELECT COUNT(*) `total count`, SUM(transaction_status = 'success') `success`, ";
$sql .=" SUM(transaction_status = 'inprocess') `inprocess`, SUM(transaction_status = 'fail') `fail`, ";
$sql .=" SUM(transaction_status = 'cancelled') `cancelled` FROM user_transaction ";
$sql .=" WHERE transaction_date >= '".$start_date."' AND transaction_date <= '".$end_date."' GROUP BY transaction_date ";
$this->mDb->Query( $sql);
$queryResult = $this->mDb->FetchArray();
}
Thanks in advance.
Use FROM_UNIXTIME(transaction_date) to get it as date type. Info
Make change in your last line of query and replace with below code:
$sql .=" WHERE DATE_FORMAT(transaction_date,'%d-%m-%Y') >= '".$start_date."' AND DATE_FORMAT(transaction_date,'%d-%m-%Y') <= '".$end_date."' GROUP BY FROM_UNIXTIME(transaction_date)";
And this link will be more helpful to you MySQL convert datetime to Unix timestamp.

PHP / MySQL query - if current time/date is in schedule

I'm trying to develop a simple php based schedule ...
This is my database table:
Let's say that current time/date is: 2012-03-21 02:00:00
PHP script should echo: Meeting 2
I have a part of the script, but no idea what to do next
<?php
$con = mysql_connect("localhost","root","");
if (!$con){die('Could not connect: ' . mysql_error());}
mysql_select_db("schedule", $con);
$current_time = date("Y-m-d H:i:s");
$result = mysql_query("SELECT * FROM events");
while($row = mysql_fetch_array($result)){
echo $row['Subject'];
}
mysql_close($con);
?>
How to filter query, to mach current time and scheduled subject? Thank you!
(if current_time is between StartTime and EndTime - echo it's Subject)
Try this query
SELECT * FROM events WHERE NOW() BETWEEN StartTime AND EndTime
Try select * from events where StartTime <= NOW() and EndTime >= NOW()
NOW() will give you current time in mysql.
Please try the following query
select * from events where current_time between startTime and endTime
Your query should like this
$result = mysql_query("SELECT * FROM events WHERE current_time between startTime and endTime);
The mysql BETWEEN operator may be of some use.
$startDate = gmdate('Y/m/d/ H:i:s','2012-03-21 02:00:00');
$endDate = gmdate('Y/m/d/ H:i:s','2012-03-21 02:23:59');
$sql = 'select subject from events
where startTime between "'.gmdate("Y/m/d H:i:s", $startDate) .
'" and "' . gmdate("Y/m/d H:i:s", $endDate).'") ';
Firstly, you're going to have to convert your StartTime and EndTime into a timestamp using strtotime so you can compare it to the current time (same thing goes for current time):
http://php.net/manual/en/function.strtotime.php
Then just say if current_time > StartTime AND current_time < EndTime echo Subject.
Something like this should work:
while($row = mysql_fetch_array($result)){
$startTime = strtotime($row['StartTime']);
$endTime = strtotime($row['StartTime']);
if (strtotime($current_time) > $startTime && strtotime($current_time) < $endTime
echo $row['Subject'];
}
putenv("TZ=Asia/Calcutta"); // To Set TimeZone
$now = date("Y-m-d H:i:s");
$sql = "select * from events where '$now' between StartTime and EndTime";

Selecting new records by datetime

I'm trying to select records which have a recdate within the past year
$goodate = date('Y-m-d h:i:s', mktime(0, 0, 0, date("m"), date("d"), date("Y")-1));
$sqlstmt = "SELECT * FROM #__mytable WHERE id=".$uid." AND recdate > ".$goodate.'"' ;
but I'm getting no records.
What am I doing wrong?
$sqlstmt = "SELECT * FROM #__mytable
WHERE id=".$uid."
AND recdate > ADDDATE(CURDATE(), INTERVAL -1 YEAR)";
If you want to do this programmatically, the DateTime object is a great tool.
$date = new DateTime();
$gooddate = $date->sub(DateInterval::createFromDateString('1 year'));
$sql = "SELECT * FROM #__mytable WHERE id=".$uid." AND recdate > ".$goodate->format('Y-m-d').'"';
This is assuming your date field is mysql type DATE. If not, there are similar DateTime methods for outputting unix timestamps, and the format() method accepts any formatting string that date() does.
$sql = "
select *
from #__mytable
where
id = $uid
and
date_format(recdate, '%Y') = date_format(adddate(curdate(), interval -1 year), '%Y')
"

Categories