mysql2date()将mysql格式的日期翻译成php函数date()可接收的格式

目录

描述

译文

将mysql格式的日期翻译成php函数date()可接收的格式

原文

Converts given MySQL date string into a different format.

mysql2date() 描述

用法

<?php $date mysql2date$format$date$translate ); ?>

mysql2date() 用法

参数

$format

(string) (必填) The requested output format, should be either a PHP date format string, e.g. 'U' for a Unix timestamp, or 'G' for a Unix timestamp assuming that $date is GMT.

默认值: None

$date

(string) (必填) the input string, this cannot be the raw time-stamp, it has to be converted into the following format first: 'Y-m-d H:i:s' . This is the only way mysql2date() will recognize your date.

默认值: None

$translate

(boolean) (可选) If true then the given date and format string will be passed to date_i18n() for translation.

默认值: true

mysql2date() 参数

返回值

(string|integer

Formatted date string, or Unix timestamp.

mysql2date() 返回值

示例

Convert a MySQL date to a Unix timestamp:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
echo mysql2date( 'U', '2012-02-23 06:12:45' ); // 1329977565

Convert a MySQL date to another date format:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
echo mysql2date( 'l, F j, Y', '2012-02-23 06:12:45' ) // Thursday, February 23, 2012

mysql2date() 示例

历史

添加于 版本: 0.71

mysql2date() 历史

源文件

mysql2date() 函数的代码位于 wp-includes/functions.php

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Convert given date string into a different format.
 *
 * $format should be either a PHP date format string, e.g. 'U' for a Unix
 * timestamp, or 'G' for a Unix timestamp assuming that $date is GMT.
 *
 * If $translate is true then the given date and format string will
 * be passed to date_i18n() for translation.
 *
 * @since 0.71
 *
 * @param string $format    Format of the date to return.
 * @param string $date      Date string to convert.
 * @param bool   $translate Whether the return date should be translated. Default true.
 * @return string|int|bool Formatted date string or Unix timestamp. False if $date is empty.
 */
function mysql2date( $format, $date, $translate = true ) {
	if ( empty( $date ) )
		return false;

	if ( 'G' == $format )
		return strtotime( $date . ' +0000' );

	$i = strtotime( $date );

	if ( 'U' == $format )
		return $i;

	if ( $translate )
		return date_i18n( $format, $i );
	else
		return date( $format, $i );
}

mysql2date() 源文件

相关