$valid is An array and $createdon and $days are get the Date value from array.
My select query is not work with these date value of PHP variable please solve its.
function checkexpiretime($valid)
{
$db = &JFactory::getDBO();
foreach($valid as $validity)
{
echo $createdon=$validity->CreatedOn;
echo $days=$validity->days;
}
echo $query="SELECT * FROM jos_generatekey WHERE CreatedOn BETWEEN DATE_ADD('".$createdon."',".INTERVAL $days DAY.")". "AND CURDATE()";
$db->setQuery($query);
$checkexpire = $db->loadObjectList();
print_r($checkexpire);
return $checkexpire;
}
Query should be like this:
$query= "SELECT * FROM jos_generatekey WHERE CreatedOn BETWEEN DATE_ADD('".$createdon."',INTERVAL ". $days ." DAY) AND CURDATE()";
Related
I'm building a query so I can get a list of new users by month:
public function getNewUsers($month) {
$month = date('F', strtotime($month));
$sql = "SELECT * FROM `{$this->table}` WHERE MONTHNAME(`account_creation_date`) = {$month} ORDER BY `id` DESC";
$stmt = $this->pdo->prepare($sql);
$stmt->execute([$month]);
if ($stmt->rowCount() == 0) return null;
$users = $stmt->fetchAll(PDO::FETCH_NAMED);
foreach ($users as &$user) {
$user = $this->applyRelations($user);
}
return $users;
}
An example of "account_creation_date" value is "08/08/2019 4:55 pm"
Im trying to pass in a month name like "August" and have the query convert the account_creation_date values to their month names.
Although I'm getting no errors, nothing is returning when the query is executed.
How can I accomplish this? Is MONTHNAME what I want?
Thanks!
This question already has answers here:
How do I query between two dates using MySQL?
(12 answers)
Closed 6 years ago.
I am trying to fetch some data from database based on user entered month and year and table name.
From month and year I calculate from_date and to_date
but query is not working if I put dates between $from_date and $todate.
$tableName = $_REQUEST['tableName'];
$month = $_REQUEST['monthName'];
$year = $_REQUEST['yearName'];
// echo json_encode($tableName);
$tableName = json_encode($tableName);
//echo $tableName;
$from_date = date('Y-m-d',strtotime($year."-".$month."-01"));
//echo json_encode($from_date);
//$to_date = date('Y-m-d',strtotime($year."-".$month."-01"));
$to_date = date('Y-m-t', strtotime($from_date));
//echo json_encode($to_date);
$conn = new PDO("sqlite:../../assets/rule_data.db");
$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//$sqlQuery = "select * from $tableName WHERE date >= '".$from_date."' AND date <= '".$to_date."' ";
//$sqlQuery = "SELECT * FROM $tableName WHERE v_cr_sysdate >= '".$from_date."' AND v_cr_sysdate <= '".$to_date."' ";
//$sqlQuery = "SELECT * FROM $tableName";
//$sqlQuery = "select * from $tableName WHERE date >= '".convert('$from_date','%d-%m-%y')."' AND date <= '".date($to_date)."' ";
$sqlQuery = "select * from $tableName WHERE date between '". date('Y-m-d', strtotime($from_date))."' ";
$sqlQuery .= " AND date <='". date('Y-m-d', strtotime($to_date))."' ";
$query = $conn->query($sqlQuery);
echo json_encode($query);
echo json_encode(["riskModules"=>$query->fetchAll(PDO::FETCH_ASSOC)]);
If I remove AND consition and keep only where date >= '$from_date' It will work but not with date range of from and to date.
Please help where I am wrong in giving AND query to where clasuse.
You can use BETWEEN clause to replace a combination of "greater than
equal AND less than equal" conditions.
Changes
$sqlQuery = "select * from $tableName WHERE date between '". date('Y-m-d', strtotime($from_date))."' ";
$sqlQuery .= " AND '". date('Y-m-d', strtotime($to_date))."' ";
(OR) Which Is Similar As
$sqlQuery = "select * from $tableName WHERE date >= '". date('Y-m-d', strtotime($from_date))."' ";
$sqlQuery .= " AND date <= '". date('Y-m-d', strtotime($to_date))."' ";
Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
Quick Links
mysql-between-clause
The SQL BETWEEN Operator
I have a function that lists all the users that registered today but for some reason it returns an empty result
function get_user_by_date($start=null,$end=null){
global $wpdb;
if ($start == null && $end == null){
$end = date("Y-m-d");
}
echo $end;
$query = "SELECT * FROM wp_users WHERE user_registered >= %s";
$prep = $wpdb->prepare($query,$end);
echo $prep;
$results = $wpdb->get_results($prep);
return $results;
}
Query looks like this when I tried to echo prepared statement
SELECT * FROM wp_users WHERE user_registered >= '2015-02-27'
When I use this query in my PHPMyAdmin, it works fine. Can you please tell me what I am doing wrong?
Try to:
$query = "SELECT * FROM wp_users WHERE user_registered >= DATETIME(%s)";
and here:
$end = date('Y-m-d H:i:s');
I am trying to get the records per current day, week and month in php mysql. The date column is of date type. The issue is here is that my week records and months records are getting same from below script. Here is my code, have a look.
public function getTodayComing(){
$connection = db::factory('mysql');
$sql = "select * from bookings,bookers where ";
$qualifier = ' bookings.booker_id = bookers.id
AND ((status ="'.AppGlobal::$bookingStatus['APPROVE'].'"
OR status ="'.AppGlobal::$bookingStatus['RESCHEDULED'].'"
OR status ="'.AppGlobal::$bookingStatus['RECONSULTED'].'")
AND date=DATE( NOW() ))
ORDER BY date ASC';
$sql. = $qualifier;
return $valuearray = $connection->getArray ($sql );
}
public function getWeekComing() {
$connection = db::factory('mysql');
$sql = "select * from bookings,bookers where ";
$qualifier = ' bookings.booker_id = bookers.id
AND ((status ="'.AppGlobal::$bookingStatus['APPROVE'].'"
OR status ="'.AppGlobal::$bookingStatus['RESCHEDULED'].'"
OR status ="'.AppGlobal::$bookingStatus['RECONSULTED'].'")
AND date > DATE_SUB(NOW(), INTERVAL 1 WEEK)) ORDER BY date ASC';
$sql. = $qualifier;
return $valuearray = $connection->getArray( $sql );
}
public function getMonthComing() {
$connection = db::factory('mysql');
$sql = "select * from bookings,bookers where ";
$qualifier = ' bookings.booker_id = bookers.id
AND ((status ="'.AppGlobal::$bookingStatus['APPROVE'].'"
OR status ="'.AppGlobal::$bookingStatus['RESCHEDULED'].'"
OR status ="'.AppGlobal::$bookingStatus['RECONSULTED'].'")
AND date > DATE_SUB(NOW(), INTERVAL 1 MONTH)) ORDER BY date ASC';
$sql. = $qualifier;
return $valuearray = $connection->getArray( $sql );
}
Aah, I just saw it I think
date > DATE_SUB(...)
should be
date BETWEEN CUR_DATE() AND DATE_ADD(CUR_DATE(), INTERVAL 1 ...)
I'm using the following code to sort MySQL queries into time/date:
mysql_select_db("user_live_now", $con);
$result = mysql_query("SELECT * FROM users_newest_post ORDER BY users_date_post DESC");
while($row = mysql_fetch_array($result))
{
print($row['user']);
}
instead of having the PHP run through and show all the values in the table can I have it show the values from an array?
So, you want to find specific users in the SQL query to return? Build your query programmatically:
$users = array('User1','John','Pete Allport','etc');
$sql = "SELECT * FROM `users_newest_post` WHERE ";
$i = 1;
foreach($users as $user)
{
$sql .= "`username` = '$user'";
if($i != count($users))
{
$sql .= " OR ";
}
$i++;
}
$sql .= " ORDER BY `users_date_post` DESC";
$result = mysql_query($sql);
Which would get you a query like:
SELECT * FROM `users_newest_post`
WHERE `username` = 'User1'
OR `username` = 'John'
OR `username` = 'Pete Allport'
OR `username` = 'etc'
ORDER BY `users_date_post`
DESC
So, you want to find all posts for a certain date or between two dates, kinda hard to do it without knowing the table structure, but you'd do it with something like this:
//Here's how to find all posts for a single date for all users
$date = date('Y-m-d',$timestamp);
//You'd pull the timestamp/date in from a form on another page or where ever
//Like a calendar with links on the days which have posts and pass the day
//selected through $_GET like page.php?date=1302115769
//timestamps are in UNIX timestamp format, such as you'd get from time() or strtotime()
//Note that, without a timestamp parameter passed to date() it uses the current time() instead
$sql = "SELECT * FROM `posts` WHERE `users_date_post` = '$date'"
$results = mysql_query($sql);
while($row = mysql_fetch_assoc($results))
{
echo $row['post_name'] . $row['users_date_post']; //output something from the posts
}
//Here's how to find all posts for a range of dates
$startdate = date('Y-m-d',$starttimestamp);
$enddate = date('Y-m-d',$endtimestamp);
//Yet again, date ranges need to be pulled in from somewhere, like $_GET or a POSTed form.
//Can also just pull in a formatted date rather than a timestamp and use it straight up instead, rather than going through date()
$sql = "SELECT * FROM `posts` WHERE `users_date_post` BETWEEN '$startdate' AND '$enddate'";
//could also do:
//"SELECT * FROM `posts` WHERE `users_date_post` > '$startdate' AND `users_date_post` < '$endate'"
$results = mysql_query($sql);
while($row = mysql_fetch_assoc($results))
{
//output data
}
To find posts for a specific user you would modify the statement to be something like:
$userid = 5; //Pulled in from form or $_GET or whatever
"SELECT * FROM `posts` WHERE `users_date_post` > '$startdate' AND `users_date_post` < '$enddate' AND `userid` = $userid"
To dump the result into an array do the following:
mysql_select_db("user_live_now", $con);
$result = mysql_query("SELECT * FROM users_newest_post ORDER BY users_date_post DESC");
while($row=mysql_fetch_assoc($result))
{
$newarray[]=$row
}
What you probably want to do is this:
$users = array("Pete", "Jon", "Steffi");
$users = array_map("mysql_real_escape_string", $users);
$users = implode(",", $users);
..("SELECT * FROM users_newest_post WHERE FIND_IN_SET(user, '$users')");
The FIND_IN_SET function is a but inefficient for this purpose. But you could transition to an IN clause with a bit more typing if there's a real need.
$sql = 'SELECT * FROM `users_newest_post` WHERE username IN (' . implode(',', $users) . ')';