select date that is less or equal to current date - php

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.

Related

how to change the format of curdate()

i want to display the total value for the current month. in my date field (entrydate), i am using the m/d/Y format; however, i have try using the following code but it's not working.please help
$query8 ="SELECT amount FROM pgoods WHERE MONTH(entrydate) = MONTH(CURDATE()) YEAR(entrydate) = YEAR(CURDATE())";
$res = mysqli_query($mysqli, $query8);
$amou = '';
while($row = mysqli_fetch_array($res)){
$amount1= "".$row["amount"]."";
$amou += $amount1;
}
try this it will give you only month
DATE_FORMAT(CURDATE(), '%M %Y')

SELECT SUM with date variable, for prestashop

i'd like to have a result on PHP/MYSQL
I have a table ps_orders with price on total_paid
I need to ask total for all price in current date, i have dificult ti insert correct date. I'm stopping here, and do not works... thnaks
....
$date = date("Y-m-d");
$query = "SELECT SUM(total_paid) FROM ps_orders WHERE delivery_date = '%$date%'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_assoc($result);
echo mysql_result($result, 0);
Try
$query = "SELECT SUM(total_paid) FROM ps_orders WHERE delivery_date = '$date'";
or
$query = "SELECT SUM(total_paid) FROM ps_orders WHERE delivery_date LIKE '%$date%'";
depending on how your delivery_date field is setup as well as what you are using in your $date variable (time stamp vs just m-d-y), your query up to the where clause looks okay, but you could also try:
SELECT SUM(total_paid)
FROM ps_orders
WHERE delivery_date = $date;
if you are using a datetime field for delevery_date, you'll have to go more in depth and use a range:
SELECT SUM(total_paid)
FROM ps_orders
where (delivery_date > $date
and deliver_date < $date +interval 1 day)
This link should also help you out quite a bit when working with date: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add
You can use one of the following:
DATE function in mysql converts 2013-11-20 10:54:12 to 2013-11-20, i.e. truncated a time in date
$query = "SELECT SUM(total_paid) FROM ps_orders WHERE DATE(delivery_date) = '$date'";
or you use only one '%' after $date with using LIKE, so this value will be matched for dates like 2013-11-20 10:12:13 :
$query = "SELECT SUM(total_paid) FROM ps_orders WHERE delivery_date like '$date%'";
or use string mysql function SUBSTRING
$query = "SELECT SUM(total_paid) FROM ps_orders WHERE SUBSTRING(delivery_date1, 1, 10) = '$date'";
use datetime with time and BETWEEN mysql comparison operator:
$query = "SELECT SUM(total_paid) FROM ps_orders WHERE delivery_date1 between '$date 00:00:00' and '$date 23:59:59'

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.

mysql check if uid record exist for today based on timestamp else do an insert

Im trying to do a mysql check if a record from $uid exist from today based on $timestamp and if it doesnt then do an INSERT.
//EXAMPLE RECORD FROM TABLE VOTE
--- #vote_fb_uid# --- #vote_time#
665414807 1369219044
tjt
//STEP 1 - do a look up on $uid and check with timestamp $today
$timestamp = $this->time;
$date = date('Y-m-d', $timestamp);
$today = date('Y-m-d');
$sql = "
SELECT * FROM vote WHERE
vote_fb_uid = '$this->fb_uid',
WHERE vote_time = '$CHECK_IF_THERE_IS_AN_ENTRY_FROM_TODAY'";
$res = mysql_query($sql) or die( mysql_error());
//STEP 2 - If no records are found for today - then we do an INSERT
if($no_record_for_today) {
$sql = sprintf("
INSERT INTO vote(
vote_fb_uid,
vote_time)
VALUES ('%s','%s')",
mysql_real_escape_string($this->fb_uid),
mysql_real_escape_string($this->time));
$res = mysql_query($sql) or die( mysql_error());
}
Obviously im strugling with the SQL part for the look up - im wondering if there isnt some in-built SQL function to do this or similar?
to check if you had a vote in the last 24 hours :
SELECT *
FROM vote
WHERE vote_fb_uid = '$this->fb_uid'
AND FROM_UNIXTIME(vote_time) >= DATE_SUB(NOW(), INTERVAL 1 DAY)
if you want to limit to the same day (mean you are allowed to post at 2013.05.21 23:55 and 2013.05.22 00:05)
SELECT *
FROM vote
WHERE vote_fb_uid = '$this->fb_uid'
AND DATE(FROM_UNIXTIME(vote_time)) = DATE(NOW())
CURDATE()
Returns the current date as a value in 'YYYY-MM-DD' or YYYYMMDD format, depending on whether the function is used in a string or numeric context.
mysql> SELECT CURDATE();
-> '2008-06-13'
mysql> SELECT CURDATE() + 0;
-> 20080613
Try this:
$today = date('Y-m-d'); //change it to timestamp if you want in timestamp
$sql = "
SELECT count(*) as total FROM vote WHERE
vote_fb_uid = '$this->fb_uid' and
vote_time = '$today'";
$res = mysql_query($sql) or die( mysql_error());
if($res[0]['total'] < 1){
$sql = sprintf("
INSERT INTO vote(
vote_fb_uid,
vote_time)
VALUES ('%s','%s')",
mysql_real_escape_string($this->fb_uid),
mysql_real_escape_string($this->time));
$res = mysql_query($sql) or die( mysql_error());
} else{
//return error("custom","","Already Inserted.");
echo "already inserted";
}
Your $sql query have a syntax error, you have used two times clause WHERE the correct syntax to use two or more clauses in where is using AND to join them, to get only record wich don't have an entry for today you can use DATE_SUB with 1 day interval
SELECT *
FROM vote
WHERE vote_fb_uid = '$this->fb_uid',
AND vote_time <= DATE_SUB(vote_time, INTERVAL 1 DAY)

Retrieve data from mysql on date selection

$query = "select * from researchsub_form where flag='1' and date between '$date' and '$date1' ";
I have two input, date and date1, I have to show the data from database between the date 'date' and 'date1' of flag='1', for this I use a SQL query, but I want to show the data when I enter only one date input which is either 'date' or 'date1'
You can create your condition where in dynamic.
$query = "select * from researchsub_form where flag='1'";
if(isset($date) && isset($date1))
$condition += " and date between '$date' and '$date1'";
else if(isset($date))
$condition += " and date > '$date'";
else if(isset($date1))
$condition += " and date < '$date1'";
$query += $condition;
Or you can assign to date the smalles date (1753), to date1 the biggest (9999) when one of them is empty, but its not good.
select * from researchsub_form where flag='1' and (date between '$date' and '$date1' or date='$date' or date='$date1')

Categories