php日期范围仅选择一年的数据

时间:2018-01-08 11:44:03

标签: php mysql date

我有一个php代码,它从数据库中选择从startdate到enddate的项目。但是,如果我在2017年的日期和2017年的另一个日期之间进行选择,它可以正常工作,如果我在2018年的日期和2018年的另一个日期之间进行选择,它可以正常工作,但如果我选择2017年到2018年之间,它不工作

以下是我的代码

/// this is the php selecting
if (array_key_exists('generate', $_POST)){
 $startdate = ($_POST['startdate']);
 $enddate = ($_POST['enddate']);
 $getoutletid = ($_POST['outletid']);
    }

//then this is the query
$q = "
SELECT * 
  FROM ".TBL_SALES_LOGS." 
 WHERE dateofsales BETWEEN '$startdate' and '$enddate' 
 ORDER 
    BY timestamp
    "

当我在同一页面上回显时,这是我的代码的返回日期格式 从2017/06/11到01/09/2018

1 个答案:

答案 0 :(得分:1)



<?php

echo('this will work only you stored in tables unix time as time() provide :'.time().'<hr>');
$july=mktime(0, 0, 0, 7, 1, 2017);

$startdate=mktime(0, 0, 0, 1, 1, 2017);//from first jannuary 2017 =you should use the date from were you define the time interval

$enddate=mktime(0, 0, 0, 1, 1, 2018);// first jannuary 2018 =you should use the date until you define the time interval

if(($july>=$startdate)and($july<=$enddate)) print('Success ,the date='.date('l jS \of F Y h:i:s A',$july).' provided as time() does =Unix timestamp IS IN CHOOSED INTERVAL<BR>');else print('the date ='.date('l jS \of F Y h:i:s A',$july).' provided doesn`t below to interval');
echo('<hr>');
$q = '
SELECT * 
  FROM ".TBL_SALES_LOGS." 
 WHERE dateofsales BETWEEN '.$startdate.' and '.$enddate.' 
 ORDER 
BY timestamp
';

echo($q.'<hr>');

$q = '
SELECT * 
  FROM ".TBL_SALES_LOGS." 
 WHERE dateofsales <= '.$startdate.' and dateofsales >='.$enddate.' 
 ORDER 
BY timestamp
';

echo($q.'<hr>');



?>
&#13;
&#13;
&#13;

输出:

&#13;
&#13;
this will work only you stored in tables unix time as time() provide :1515415011<hr>Success ,the date=Saturday 1st of July 2017 12:00:00 AM provided as time() does =Unix timestamp IS IN CHOOSED INTERVAL<BR><hr>
SELECT * 
  FROM ".TBL_SALES_LOGS." 
 WHERE dateofsales BETWEEN 1483225200 and 1514761200 
 ORDER 
    BY timestamp
    <hr>
SELECT * 
  FROM ".TBL_SALES_LOGS." 
 WHERE dateofsales <= 1483225200 and dateofsales >=1514761200 
 ORDER 
    BY timestamp
    <hr>
&#13;
&#13;
&#13;