【问题标题】:php date range selecting data for a year only [closed]php日期范围仅选择一年的数据[关闭]
【发布时间】:2018-01-08 11:44:02
【问题描述】:

我有一个 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 年 11 月 6 日到 2018 年 1 月 9 日

【问题讨论】:

  • 请举几个例子'startdate' / 'enddate' - 值。
  • 你能发布表结构吗?和确切的例子(开始和结束日期),这是行不通的
  • 您将什么日期格式传递给 SQL 查询?
  • @Ademaintain 请使用您的表结构详细信息更新您的帖子。此外,更新您获得正确输出的开始和结束日期

标签: php mysql date


【解决方案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>');



?>

输出:

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>

【讨论】:

    猜你喜欢
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-02
    • 2018-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多