code

PHP를 사용하여 MySQL 시간을 UNIX 타임 스탬프로 변환하는 방법은 무엇입니까?

codestyles 2020. 10. 28. 08:06
반응형

PHP를 사용하여 MySQL 시간을 UNIX 타임 스탬프로 변환하는 방법은 무엇입니까?


'UNIX timestamp to MySQL time'에 대해 묻는 질문이 많이 있습니다. 역방향이 필요 했어, 그래 ... 어떤 생각?


사용 strtotime(..):

$timestamp = strtotime($mysqltime);
echo date("Y-m-d H:i:s", $timestamp);

또한 이것을 확인하십시오 (MySQL 방식으로 수행하십시오.)

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp


UNIX_TIMESTAMP쿼리에서 직접 mysql의 함수를 사용할 수 있습니다 . 예는 다음과 같습니다.

SELECT UNIX_TIMESTAMP('2007-11-30 10:30:19');

마찬가지로 date / datetime 필드를 전달할 수 있습니다.

SELECT UNIX_TIMESTAMP(yourField);

내 다른 게시물 중 하나에서 unixtimestamp를 얻습니다.

$unixTimestamp = time();

mysql datetime 형식으로 변환 :

$mysqlTimestamp = date("Y-m-d H:i:s", $unixTimestamp);

mysql 타임 스탬프 얻기 :

$mysqlTimestamp = '2013-01-10 12:13:37';

unixtimestamp로 변환 :

$unixTimestamp = strtotime('2010-05-17 19:13:37');

... 사용자가 실제 시간을 입력했는지 확인하기 위해 한 번 또는 여러 번의 시간과 비교 :

if($unixTimestamp > strtotime("1999-12-15") && $unixTimestamp < strtotime("2025-12-15"))
{...}

유닉스 타임 스탬프도 더 안전합니다. 예를 들어 이전 범위 확인을 확인하기 전에 URL 전달 변수가 유효한지 확인하려면 다음을 수행 할 수 있습니다.

if(ctype_digit($_GET["UpdateTimestamp"]))
{...}

$time_PHP = strtotime( $datetime_SQL );

약간 줄여서 ...

echo date("Y-m-d H:i:s", strtotime($mysqltime));

대신 PHP와 함께 strtotime사용해야합니다 DateTime. 다음과 같이 시간대를 고려할 수도 있습니다.

$dt = DateTime::createFromFormat('Y-m-d H:i:s', $mysqltime, new DateTimeZone('Europe/Berlin'));
$unix_timestamp = $dt->getTimestamp();

$mysqltime예를 들어, MySQL Datetime 유형 2018-02-26 07:53:00입니다.

참고 URL : https://stackoverflow.com/questions/4577794/how-to-convert-mysql-time-to-unix-timestamp-using-php

반응형