php PHP中的curdate()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15909857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:10:53  来源:igfitidea点击:

curdate() in PHP

phpmysql

提问by Blahwhore

i'm looking to implement something that will give off called "curdate()" function when executed on mysql.

我希望实现一些在 mysql 上执行时会发出称为“curdate()”函数的东西。

However the problem is, whenever i try to add "curdate()" as a function in PHP

然而问题是,每当我尝试在 PHP 中添加“curdate()”作为函数时

$today = curdate();

It won't work (of course haha.)

它不会工作(当然哈哈。)

So, my question (rephrased) is...

所以,我的问题(改写)是......

How do i make it so curdate() is a PHP function (rather than just an SQL one.)

我如何使它成为一个 PHP 函数(而不仅仅是一个 SQL 函数。)



I attempted to use

我试图使用

$today = date("Y-m-d");

but for some reason it won't work, i need to add something i guess.

但由于某种原因它不起作用,我需要添加一些我猜的东西。

(As a side note, i'm using the "profile.class.php" file, so perhaps that'll explain why "$today = date("Y-m-d");" won't work.)

(作为旁注,我使用的是“profile.class.php”文件,所以也许这可以解释为什么“$today = date("Ymd");”不起作用。)



My Profile.class.php config

我的 Profile.class.php 配置



function add_viewed_me($member_id,$member_viewed)
{
                $time_now = date("Y-m-d");          

    $sql="insert into view_me";
    $sql.="(member_id";
    $sql.=", member_viewed";
    $sql.=", last_viewed_on";
    $sql.=", num_viewed)";
    $sql.=" values($member_id";
    $sql.=", $member_viewed";
    $sql.=", '$time_now'";
    $sql.=", 1)";

    $res=mysql_query($sql);

    if($res)
    {
        return 1;
    }
    else
    {
        return 0;
    }           
}       

回答by kylex

You just want a PHP function that returns the current date?

您只想要一个返回当前日期的 PHP 函数?

function curdate() {
    return date('Y-m-d');
}

// echo the date to screen
echo curdate();

EDIT:

编辑:

You need to edit your query string directly:

您需要直接编辑查询字符串:

$sql.= ", CURDATE()";instead of $sql.=", '$time_now'";

$sql.= ", CURDATE()";代替 $sql.=", '$time_now'";

回答by Kermit

Just use MySQL's CURDATE()function; no need for PHP functions.

用MySQL的CURDATE()功能就行了;不需要 PHP 函数。

$sql="insert into view_me (member_id, member_viewed, last_viewed_on, num_viewed)
      values($member_id, $member_viewed, CURDATE(), 1)";