简体   繁体   English

PHP-在dd / mm / yyyy之间进行SQL搜索,同时具有yyyy-mm-dd hh:mm:ss

[英]PHP - SQL search between dd/mm/yyyy while having yyyy-mm-dd hh:mm:ss

I want to show data that from between date that I choose. 我想显示选择的日期之间的数据。

Already tried SELECT * from myTable WHERE myDATE BETWEEN LIKE 'datefrom%' AND LIKE 'dateend%' and for CONVERT I still don't get it because haven't found one that have a full query about that. 已经尝试SELECT * from myTable WHERE myDATE BETWEEN LIKE 'datefrom%' AND LIKE 'dateend%' ,对于CONVERT,我仍然没有得到它,因为还没有找到对此有完整查询的人。

As my title, my sql field is datetime type and with format 'yyyy-mm-dd hh:mm:ss' while in php I have format dd/mm/yyyy 作为我的标题,我的sql字段是日期时间类型,格式为“ yyyy-mm-dd hh:mm:ss”,而在php中,格式为dd / mm / yyyy

Please answer this only in PHP code and SQL query and in full mode. 请仅在PHP代码和SQL查询以及完整模式下回答此问题。

Thank you :) 谢谢 :)

You can do between YYYY-MM-DD and YYYY-MM-DD (excluding time) in MySQL but MySQL won't understand dd/mm/yyyy so you need to convert it to the proper format. 您可以在MySQL中的YYYY-MM-DDYYYY-MM-DD (不包括时间)之间进行操作,但是MySQL无法理解dd/mm/yyyy因此您需要将其转换为正确的格式。

When you exclude the time, it interprets it at 00:00:00 so depending on your situation, you may want to do between YYYY-MM-DD and YYYY-MM-DD 23:59:59 当您排除时间时,它会在00:00:00解释,因此根据您的情况,您可能需要在YYYY-MM-DDYYYY-MM-DD 23:59:59

Note: PHP won't properly interpret dd/mm/yyyy either. 注意:PHP也不会正确解释dd/mm/yyyy PHP date functions understand mm/dd/yyyy and dd-mm-yyyy . PHP日期函数了解mm/dd/yyyydd-mm-yyyy

As commenters have said, you will first have to convert your dd/mm/yyyy formatted date to something that MySQL prefers, which is YYYY-mm-dd H:i:s 正如评论员所说,您首先必须将dd / mm / yyyy格式的日期转换为MySQL首选的日期,即YYYY-mm-dd H:i:s

So, how do you convert your date to MySQL friendly date? 那么,如何将日期转换为MySQL友好日期? Do something like this: 做这样的事情:

<?php
$datefrom = '25/06/2015';
$dateend = '20/10/2015';

// create an array with dd, mm and yyyy
$splitFrom = explode('/', $datefrom);
$splitTo = explode('/', $dateend);

// mktime takes hour, minutes, seconds, month, day, year arguments
// create timestamp using mktime and pass it to date and format it to
// mysql friendly timestamp
$mysqlFrom = date('Y-m-d H:i:s',  mktime(0, 0, 0, $splitFrom[1], $splitFrom[0], $splitFrom[2]));
$mysqlTo = date('Y-m-d H:i:s',  mktime(0, 0, 0, $splitTo[1], $splitTo[0], $splitFrom[2]));

// echo sprintf("From %s and To %s", $mysqlFrom, $mysqlTo);

// this is where you would do your SQL statement and substitute $mysqlFrom and $mysqlTo dates

?>

Note that this is just a rudimentary example to show you how you can proceed with solving the problem yourself. 请注意,这只是一个基本示例,向您展示如何自己继续解决问题。 As time passes, you will have to optimize this kind of code further. 随着时间的流逝,您将不得不进一步优化此类代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM