MySQL datetime in PHP

I found the correct solution to my "problem", but even after reading the mysql pages, I do not understand the logic of this.

I currently store login information in my system in the "datetime" field in one of my tables (YYYY-MM-DD hh:mm:ss).

When I want to display data on one of my php pages, just placing the exact field data shows the format mentioned above.

I would THINK, simply using date("Y-m-d",$row["DATE"]), where $row["DATE"]matches a specific string value, will return the desired format.

Instead, I have to use: date("Y-m-d", strtotime($row["DATE"])).

Why is this? My field is $row["DATE"]not a string in the first place. Should I be able to simply reorder the data stored in the datetime field? Wasn't that the goal of restoring my entire table table to accommodate the date and time?

+3
source share
2 answers

MySQL has a built-in date_format function that you can use to display the date as you want.

SELECT DATE_FORMAT(date_field, '%Y-%m-%d') as date_field FROM table_name

The manual has a list of formats and variables needed to display it. Using this method, there will be no need for PHP to convert it, etc. In addition, there is less code on the PHP side for something that MySQL can handle easily.

EDIT

, , .

PHP date UNIX, MySQL . MySQL IE: YYYY-MM-DD HH:MM:SS, , . UNIX - 1969-2037, , " " , , , , , MySQL DATETIME , 5 .

WIKI UNIX .

+5

MySQL timestamp unix, PHP, , .

, , - unix, , ...

SELECT UNIX_TIMESTAMP(datefield) as u_datefield FROM table

timestamp, , PHP:

<?php
$showdate = date("Y-m-d",$row['u_datefield']);
?>

, unix , , 1970 2038 , , .

, , .

+2

Source: https://habr.com/ru/post/1766033/


All Articles