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')
"
Related
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
I am using below jQuery:
$rezultat = "SELECT sum(vrednost) as vrednost FROM vrednosti WHERE username = '$username' AND cas between '".date("Y-".(date("m")-1)."-01")."' AND '".date("Y-".(date("m")-1)."-31 23:59:59")."' ";
This works fine, where previous month is in the same year as current date.
But now I run into trouble, because can't echo december, which is different year (2015) than january (2016).
Is there any elegant solution?
You can use strtotime function to get previous month date, pass that time value to date function as second argument like date("Y-m-d", strtotime("today -1 month")), also to get the last date of any month by changing the date format in date function argument, e.g. to get last date of previous month you can use date("Y-m-t", strtotime("today -1 month")).
So, in your case, try this:
$rezultat = "SELECT sum(vrednost) as vrednost FROM vrednosti WHERE username = '$username' AND cas between '". date("Y-m-01", strtotime("today -1 month")) ."' AND '".date("Y-m-t", strtotime("today -1 month")) ."' ";
$rezultat = "SELECT sum(vrednost) as vrednost FROM vrednosti WHERE username = '$username' AND cas between '".date("Y-m-01", mktime(23, 59, 59, date("n")-1, date("j"), date("Y")))."' ";
I have saved rows in my table with custom timezone.Now I want to retrieve data from those table for just today.
So here is what I tried
date_default_timezone_set('America/Los_Angeles');
$dt = new DateTime();
$today = $dt->format('Y-m-d'); //outputs 2015-12-07
$ok = mysqli_query($sqli,"SELECT * FROM `table` WHERE `date` = '$today'" );
And my row contains date in timestamp format like 2015-12-07 22:42:02
But I get empty result.
Try this:
$ok = mysqli_query($sqli,"SELECT * FROM `table` WHERE DATE(date) = CURDATE()" );
to convert time according to timezone: ConvertTimeZone
if $today='2015-12-07 22:42:02'; your query will give the result.
$today='2015-12-07 22:42:02';
$ok = mysqli_query($sqli,"SELECT * FROM `table` WHERE `date` = '$today'" );
else do pass the today date and next date and retrieve the value as given
$today='2015-12-07';
$next='2015-12-08';
$ok = mysqli_query($sqli,"SELECT * FROM `table` WHERE `date` >= '$today' and `date` <= '$next' " );
for more details refer this Oracle SQL : timestamps in where clause How to compare Timestamp in where clause
You should convert date to timestamp before passing it to mysql:
date_default_timezone_set('America/Los_Angeles');
$dt = new DateTime();
$today = $dt->format('Y-m-d'); //outputs 2015-12-07
$ok = outputs("SELECT * FROM `table` WHERE DATE_FORMAT(date,'%Y-%m-%d')= $today" );
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 ";
I am working on a file management project, where I have expiry dates for every file. I need to list all the files that are going to expire the next year. What will be the SQL query?
should it be something like:
$date = date ('Y-m-j');
$newdate = strtotime ( '+1 year' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );
$sql = "SELECT * FROM `files` WHERE `expDate` = year ($newdate)" ;
Try:
$sql = "SELECT * FROM `files` WHERE YEAR(`expDate`) = YEAR(NOW()) + 1" ;
If the expDate is in mysql timestamp(yyyy-mm-dd hh:mm:ss) , you can use this
DATE_ADD(`expDate`, INTERVAL 1 YEAR)
Try this
$newdate = date ( 'Y-m-j' , strtotime('+1 year') );
$sql = "SELECT * FROM `files` WHERE `expDate` = year ($newdate)" ;
or
$sql = "SELECT * FROM `files` WHERE `expDate` = YEAR(NOW())+1;
you can use mysql DATE_ADD to add years
$sql = "SELECT * FROM `files` WHERE `expDate` = YEAR(DATE_ADD($date, INTERVAL 1 YEAR))";