How To Process MySQL Timestamp in PHP MySQLのタイムスタンプを処理する方法についてはPHP
Many of us do not realize that MySQL's Timestamp is not the timestamp which PHP date() understands.多くの問い合わせはありませんMySQLのタイムスタンプを実感してのタイムスタンプではないがPHPのdate ( )を理解します。 In fact if you try to use MySQL timestamp directly in php date() function then you will get totally wrong dates.実際にMySQLを使用しようとした場合のタイムスタンプを直接PHPのdate ( )関数の日付を選択すると、完全に間違っています。 There is a simple way to use MySQL timestamp data in php.簡単な方法を使用するには、 MySQLのタイムスタンプのデータをPHPのです。
First you need to modify the SQL query to use MySQL's UNIX_TIMESTAMP() function.第一を変更する必要がSQLクエリを使用し、 MySQLのunix_timestamp ( )関数です。 So if your SQL looks like:もししているSQLは次のよう:
SELECT author, `when` …選択著者、 `時` …
then convert it to:それを入力し、変換する:
SELECT author, UNIX_TIMESTAMP(`when`) …選択著者、 unix_timestamp ( ` ` ) …
Then use PHP's date() function to convert the unix timestamp to appropriate display format like this: PHPの日付を使用し( )関数をUnixタイムスタンプに変換するこのように適切な表示形式:
date("F j, Y, g:ia", $row[1])日付(の" F無し、 y 、グラム:イア" 、 $行[ 1 ] )
where $row[1] contains the timestamp column value.どこ$行[ 1 ]には、タイムスタンプ列の値です。
Filed under提出されて Databaseデータベース , 、 Headline Newsニュースの見出し , 、 How Toどのように , 、 Open Source Softwareオープンソースのソフトウェア , 、 PHP PHPの , 、 RDBMS RDBMSのより多くの , 、 Tech Note技術のノート , 、 Webウェブ | |
| |
RSS 2.0 RSS 2.0を | |
Trackbackトラックバック this Article |この記事|
Email this Article電子メールこの記事
You may also like to readを読むようにすることも可能 |




































March 20th, 2008 at 3:06 pm 2008年3月20日3:06 pmの
Aca va una clase que les puede ayudar un pocoバージニアするaca unaクラスqueレpuede ayudar国連poco
class FechasMysqlクラスfechasmysql{(const _formatoTimeStampMysql='Ymd H:i:s';const _formatotimestampmysql = ' YMDとh:私: s 'を;const _formatoFechaMysql='Ym-d';const _formatofechamysql = ' ym - d 'で;const _formatoHoraMysql='H:i:s';const _formatohoramysql =の'h:私: s 'を;const _separadorFechaHoraMysql=' ';const _separadorfechahoramysql = ' ' ;const _separadorFechaMysql='-';const _separadorfechamysql ='-';const _separadorHoraMysql=':';const _separadorhoramysql =':';const _pocisionFechaEnTimestamp=0;const _pocisionfechaentimestamp = 0 ;const _pocisionHoraEnTimestamp=1;const _pocisionhoraentimestamp = 1 ;const _zonaUtc='America/Bogota';const _zonautc = 'アメリカ/ボゴタ' ;const _zonaIdioma='esl';const _zonaidioma = ' esl ' ;/** / **
* Convierte el timestamp de unix a timestamp de mysql *のconvierteエルデMySQLのタイムスタンプをUNIXのタイムスタンプデ
*
* @param int[optional] timestamp de unix por defecto el actual * @ param int [オプション] UNIXのデフォルトのタイムスタンプデエル実際の
* @return string timestamp de Mysql con formato FechasMysql::_formatoTimeStampMysql * @ return次の文字列のMySQL con 、用紙のタイムスタンプデfechasmysql : : _formatotimestampmysql
*/ * /
public static function MySqlTimeStamp($unixTimestamp=null)公共の静的関数mysqltimestamp ( $ unixtimestamp =ヌル)
{ (
FechasMysql::ConfigurarZona(); fechasmysql : : configurarzona ( ) ;
if ($unixTimestamp===null)もし( $ unixtimestamp ===ヌル)
$unixTimestamp=time(); $ unixtimestamp =時間( ) ;
return date(FechasMysql::_formatoTimeStampMysql,$unixTimestamp);返却日( fechasmysql : : _formatotimestampmysql 、 $ unixtimestamp ) ;
} )
/** / **
* Convierte el timestamp de unix a time de mysql *のconvierteエルタイムスタンプデUnixの一度デMySQLの
*
* @param[optional] int timestamp de unix por defecto el actual * @ param [オプション] intタイムスタンプデエル実際のデフォルトのUNIX
* @return string time de Mysql con formato ‘H:i:s’ * @ return次の文字列時間デのMySQL con 、用紙の' h :私: s 'に
*/ * /
public static function MySqlTime($unixTimestamp=null)公共の静的関数mysqltime ( $ unixtimestamp =ヌル)
{ (
FechasMysql::ConfigurarZona(); fechasmysql : : configurarzona ( ) ;
if ($unixTimestamp===null)もし( $ unixtimestamp ===ヌル)
$unixTimestamp=time(); $ unixtimestamp =時間( ) ;
return date(FechasMysql::_formatoHoraMysql,$unixTimestamp);返却日( fechasmysql : : _formatohoramysql 、 $ unixtimestamp ) ;
} )
/** / **
* Convierte el timestamp de unix a date de mysql *のconvierteエルデMySQLの日付をUNIXのタイムスタンプデ
*
* @param int[optional] timestamp de unix por defecto el actual * @ param int [オプション] UNIXのデフォルトのタイムスタンプデエル実際の
* @return string date de Mysql con formato ‘Ymd ‘ * @ return次の文字列日付デのMySQL con 、用紙の' YMDと'
*/ * /
public static function MySqlDate($unixTimestamp=null)公共の静的関数mysqldate ( $ unixtimestamp =ヌル)
{ (
FechasMysql::ConfigurarZona(); fechasmysql : : configurarzona ( ) ;
if ($unixTimestamp===null)もし( $ unixtimestamp ===ヌル)
$unixTimestamp=time(); $ unixtimestamp =時間( ) ;
return date(FechasMysql::_formatoFechaMysql,$unixTimestamp);返却日( fechasmysql : : _formatofechamysql 、 $ unixtimestamp ) ;
} )
/** / **
* Obtiene El string de la fecha * obtieneエルの文字列デラ日付
*
* @param int[optional] timestamp de unix por defecto el actual * @ param int [オプション] UNIXのデフォルトのタイムスタンプデエル実際の
* @param string[optional] opcion si ‘F’ retorna la fecha, por defecto; de lo contrario Fecha y Hora * @ param文字列の[オプション] opcion siエディション' F 'を返しますラ日付、デフォルト;デlo contrario日付y時間
* @return string fecha o fecha y hora en una cadena * @ return次の文字列日付文字列または日付y時間アン
*/ * /
public static function ObtenerStringFechaUnix($unixTimestamp=null,$opcionString=’F')公共の静的関数obtenerstringfechaunix ( $ unixtimestamp =ヌル、 $ opcionstring =エディション' F ' )
{ (
FechasMysql::ConfigurarZona(); fechasmysql : : configurarzona ( ) ;
if ($unixTimestamp===null)もし( $ unixtimestamp ===ヌル)
$unixTimestamp=time(); $ unixtimestamp =時間( ) ;
if ($opcionString==’F')もし( $ opcionstring ==エディション' F ' )
return strftime(’ %A, %d de %B de %Y.’ ,$unixTimestamp);リターンのstrftime ( ' %を、 % d個デ% bとデ% y. ' 、 $ unixtimestamp ) ;
elseその他の
return strftime(’ %H:%M del %A, %d de %B de %Y.’ ,$unixTimestamp);リターンのstrftime ( ' % h : %メートルデル%を、 % d個デ% bとデ% y. ' 、 $ unixtimestamp ) ;
} )
/** / **
* Obtiene El string de la fecha * obtieneエルの文字列デラ日付
*
* @param String[optional] timestamp de Mysql por defecto el actual * @ param文字列の[オプション] MySQLのデフォルトのタイムスタンプデエル実際の
* @param string[optional] opcion si ‘F’ retorna la fecha, por defecto; de lo contrario Fecha y Hora * @ param文字列の[オプション] opcion siエディション' F 'を返しますラ日付、デフォルト;デlo contrario日付y時間
* @return string fecha o fecha y hora en una cadena * @ return次の文字列日付文字列または日付y時間アン
*/ * /
public static function ObtenerStringFechaMysql($mysqlTimeStamp=null,$opcionString=’F')公共の静的関数obtenerstringfechamysql ( $ mysqltimestamp =ヌル、 $ opcionstring =エディション' F ' )
{ (
if ($mysqlTimeStamp===null)もし( $ mysqltimestamp ===ヌル)
$mysqlTimeStamp=date(FechasMysql::_formatoTimeStampMysql); $ mysqltimestamp =日( fechasmysql : : _formatotimestampmysql ) ;
$unixTimestamp=FechasMysql::UnixTimeStamp($mysqlTimeStamp); $ unixtimestamp = fechasmysql : : unixtimestamp ( $ mysqltimestamp ) ;
return FechasMysql::ObtenerStringFechaUnix($unixTimestamp,$opcionString);戻りfechasmysql : : obtenerstringfechaunix ( $ unixtimestamp 、 $ opcionstring ) ;
} )
/** / **
* Convierte el timestamp de mysql a timestamp de unix *のconvierteエルデMySQLのタイムスタンプをUNIXのタイムスタンプをデ
*
* @param string timestamp de Mysql con formato ‘Ymd H:i:s’ por defecto el actual * @ param文字列のタイムスタンプデのMySQL con 、用紙の' YMDとh :私: s 'をデフォルトエル実際の
* @return int timestamp de unix * @ return次intタイムスタンプデのUNIX
*/ * /
public static function UnixTimeStamp($mysqlTimeStamp)公共の静的関数unixtimestamp ( $ mysqltimestamp )
{ (
FechasMysql::ConfigurarZona(); fechasmysql : : configurarzona ( ) ;
$arrayTimestamp=explode(FechasMysql::_separadorFechaHoraMysql, $mysqlTimeStamp); $ arraytimestamp =爆発( fechasmysql : : _separadorfechahoramysql 、 $ mysqltimestamp ) ;
$arrayFecha = explode(FechasMysql::_separadorFechaMysql,$arrayTimestamp[FechasMysql::_pocisionFechaEnTimestamp]); $ arrayfecha =爆発( fechasmysql : : _separadorfechamysql 、 $ arraytimestamp [ fechasmysql : : _pocisionfechaentimestamp ] ) ;
$arrayHora=explode(FechasMysql::_separadorHoraMysql,$arrayTimestamp[FechasMysql::_pocisionHoraEnTimestamp]); $ arrayhora =爆発( fechasmysql : : _separadorhoramysql 、 $ arraytimestamp [ fechasmysql : : _pocisionhoraentimestamp ] ) ;
return $unixTimesTamp = mktime($arrayHora[0],$arrayHora[1],$arrayHora[2],$arrayFecha[1],$arrayFecha[2],$arrayFecha[0]);戻り$ unixtimestamp = mktime ( $ arrayhora [ 0 ] 、 $ arrayhora [ 1 ] 、 $ arrayhora [ 2 ] 、 $ arrayfecha [ 1 ] 、 $ arrayfecha [ 2 ] 、 $ arrayfecha [ 0 ] ) ;
} )
private static function ConfigurarZona()プライベート静的関数configurarzona ( )
{ (
//se asegura que la zona horaria adecuada / / se asegura que周辺情報horaria adecuada
date_default_timezone_set(FechasMysql::_zonaUtc); date_default_timezone_set ( fechasmysql : : _zonautc ) ;
//Pone el idioma / /トウモロコシパンエル言語
setlocale(LC_TIME,FechasMysql::_zonaIdioma);のsetlocale ( lc_time 、 fechasmysql : : _zonaidioma ) ;
} )
} )