Select date from my db without seconds using PHP M

2020-02-13 06:34发布

Well, I save the date's with minutes and second in my database.

And I want to check if the date from my db = to date today.

I read about this, and I don't know how to use in Date_format Maybe because my Date is varchar(255)..

Thanks for helps (:

EDIT:

Thanks all (:

标签: php mysql
6条回答
\"骚年 ilove
2楼-- · 2020-02-13 06:41

Here you go:

$dbdate = '8/21/2015 11:16:12 AM'; //add your database date here
$dt = new DateTime($dbdate);
$date = $dt->format('Y-m-d');
$today = date("Y-m-d"); //get current date


//now compare your dates
if($date == $today)
{
}
查看更多
▲ chillily
3楼-- · 2020-02-13 06:42

try this code:

 DATE_FORMAT(STR_TO_DATE(t.datestring, '%d/%m/%Y'), '%Y-%m-%d')
查看更多
来,给爷笑一个
4楼-- · 2020-02-13 07:01

You need a date in date format like timestamp for an efficient search.

With this format you can make some search like :

SELECT id, date FROM data WHERE date BETWEEN TIMESTAMP("2015-08-20 23:04:00") AND TIMESTAMP("2015-08-21 12:04:00")
查看更多
\"骚年 ilove
5楼-- · 2020-02-13 07:02

You just need simple conversion when you are having ur date in varchar. Your query should be like:

SELECT DATE_FORMAT('2015-08-21 12:35:15', '%Y-%M-%d %H:%i');

OUTPUT

2015-August-21 12:35
查看更多
唯我独甜
6楼-- · 2020-02-13 07:03

SELECT DATE_FORMAT(date_created, '%Y/%m/%d') FROM sometable

查看更多
Emotional °昔
7楼-- · 2020-02-13 07:03

Its not answer, I have a small problem..

I tried it:

$dbdate = "21/08/2015 10:13";

.

   $select_last_vote = mysql_query("SELECT `Date` FROM `Vote` WHERE `IP` = '$ip' AND `VoteID` = '$VoteID' order by `Date` DESC LIMIT 1;");
            $select_last_vote = mysql_fetch_array($select_last_vote);
            $dbdate = $select_last_vote["Date"];
            $takeResult = mysql_query("SELECT DATE_FORMAT(STR_TO_DATE('$dbdate', '%d/%m/%Y' ), '%d/%m/%Y') as Date ;");
            $Result_date = mysql_fetch_array($takeResult);
            $result_date = $Result_date["Date"];
            $today = date("d/m/Y");
    if($result_date == $today)
        {
        echo "this is today $result_date, $today";
        }
        else { // there is my output: You can vote! 00/00/0000
        echo "You can vote! $result_date";
        }

Its working now (:

Thanks for all!!

查看更多
登录 后发表回答