简体   繁体   English

从 MySQL 数据库中检索时间戳值返回当前日期

[英]Retrieving timestamp value from MySQL database returns current date

When retrieving "timestamp" value from MySQL table, my php file always returns the current date, not the timestamp date in which the row was added to MySQL table.从 MySQL 表中检索“时间戳”值时,我的 php 文件总是返回当前日期,而不是将行添加到 MySQL 表的时间戳日期。

I know for sure that timestamp works right when adding new rows in MySQL, I´ve checked phpMyAdmin and values are right.我确信在 MySQL 中添加新行时时间戳可以正常工作,我已经检查过 phpMyAdmin 并且值是正确的。 But when I ask to retrieve them using php, all the entries are returned as current date, showing the date in which I´m asking MySQL to return the data, not the original timestamp value.但是当我要求使用 php 检索它们时,所有条目都作为当前日期返回,显示我要求 MySQL 返回数据的日期,而不是原始时间戳值。

My timestamp attributes in MySQL are the following ones:我在 MySQL 中的时间戳属性如下:

name: fecha / type: TIMESTAMP / null: FALSE / default value: CURRENT_TIMESTAMP名称:fecha / 类型:TIMESTAMP / null:FALSE / 默认值:CURRENT_TIMESTAMP

I´m using the following code to retrieve the timestamp values (I had to create a few variables to translate the English timestamp to Spanish):我正在使用以下代码来检索时间戳值(我必须创建一些变量才能将英文时间戳转换为西班牙语):

    $query = mysql_query ("SELECT * FROM news;");

    while ($row = mysql_fetch_array ($query)){
        
            $row_date = strtotime ($row["fecha"]);
            date ("F j, Y", $row_date);
            $dias = array("Domingo","Lunes","Martes","Miercoles","Jueves","Viernes","Sábado");
            $meses = array("Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre");
                    
    echo $dias[date('w')]." ".date('d')." de ".$meses[date('n')-1]. " del ".date('Y') ;
    }

您需要将 $row_date 传递给每个 date() 调用。

echo date('Y', $row_date) ;

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

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