UNIXタイムスタンプ、MySQLタイムスタンプ、MySQL日時(またはその他の標準的な日付と時刻の形式)を次の形式の文字列に変換する簡単な方法はありますか?
- 今日、午後6時
- 明日、午後12時30分
- 水曜日、午後4時
- 次の金曜日、11:00am
これらを何と呼ぶかわかりません-会話形式の、現在の時間に敏感な日付形式だと思いますか?
UNIXタイムスタンプ、MySQLタイムスタンプ、MySQL日時(またはその他の標準的な日付と時刻の形式)を次の形式の文字列に変換する簡単な方法はありますか?
これらを何と呼ぶかわかりません-会話形式の、現在の時間に敏感な日付形式だと思いますか?
私が知る限り、これにはネイティブ関数はありません。私はあなたが望んでいることをするための関数(の始まり)を作成しました。
function timeToString( $inTimestamp ) {
$now = time();
if( abs( $inTimestamp-$now )<86400 ) {
$t = date('g:ia',$inTimestamp);
if( date('zY',$now)==date('zY',$inTimestamp) )
return 'Today, '.$t;
if( $inTimestamp>$now )
return 'Tomorrow, '.$t;
return 'Yesterday, '.$t;
}
if( ( $inTimestamp-$now )>0 ) {
if( $inTimestamp-$now < 604800 ) # Within the next 7 days
return date( 'l, g:ia' , $inTimestamp );
if( $inTimestamp-$now < 1209600 ) # Within the next 14, but after the next 7 days
return 'Next '.date( 'l, g:ia' , $inTimestamp );
} else {
if( $now-$inTimestamp < 604800 ) # Within the last 7 days
return 'Last '.date( 'l, g:ia' , $inTimestamp );
}
# Some other day
return date( 'l jS F, g:ia' , $inTimestamp );
}
お役に立てば幸いです。
とのドキュメントを読む必要がありstrftime
ますstrtotime
UNIXタイムスタンプを自分のフォーマットに変換する例:
$time = time(); // UNIX timestamp for current time
echo strftime("%A, %l:%M %P"); // "Thursday, 12:41 pm"
MySQLの日時値を取得するには、データベースから「2010-07-15 12:42:34」として出力されると想定して、次のことを試してください。
$time = "2010-07-15 12:42:34";
echo strftime("%A, %l:%M %P"); // "Thursday, 12:42 pm"
ここで、曜日の名前の代わりに「今日」という単語を出力するには、日付が今日であるかどうかを確認するための追加のロジックを実行する必要があります。
$time = "2010-07-15 12:42:34";
$today = strftime("%Y-%m-%d");
// compare if $time strftime's to the same date as $today
if(strftime("%Y-%m-%d", $time) == $today) {
echo strftime("Today, %l:%M %P", $time);
} else {
echo strftime("%A, %l:%M %P", $time);
}
PHPの日付関数は、さまざまな方法があり、古いクラスの上に新しいクラスが作成されているため、やや面倒です。このタイプのフォーマット(私が人間に優しいフォーマットと呼んでいます)では、それを実行する独自の関数を作成する必要があります。
変換には、前述のようにstrtotime()を使用できますが、エポックタイムを処理していて、UTC GMTタイムが必要な場合は、そのためのいくつかの関数をここに示します。strtotime()は、エポック時間をローカルサーバー時間に変換します...これは、私のプロジェクトでは望まないことでした。
/**
* Converts a GMT date in UTC format (ie. 2010-05-05 12:00:00)
* Into the GMT epoch equivilent
* The reason why this doesnt work: strtotime("2010-05-05 12:00:00")
* Is because strtotime() takes the servers timezone into account
*/
function utc2epoch($str_date)
{
$time = strtotime($str_date);
//now subtract timezone from it
return strtotime(date("Z")." sec", $time);
}
function epoch2utc($epochtime, $timezone="GMT")
{
$d=gmt2timezone($epochtime, $timezone);
return $d->format('Y-m-d H:i:s');
}
このタイプのデータをデータベースから取得する場合
$time = "2010-07-15 12:42:34";
次にこれを行います
$this->db->select('DATE_FORMAT(date, "%b %D %Y")AS date');
人間の形で好きなようにデータを表示するための情報については、こちらをご覧ください
http://www.w3schools.com/SQL/func_date_format.asp
上記のコードはcodeigniter形式ですが、MYSQLのSELECTステートメントに変換するだけです。
$query = "SELECT `title`,`post`, DATE_FORMAT(date, "%a, %d %b %Y %T") AS date FROM `posts` LIMIT 0, 8 ";
ニーズに合わせて%a文字を変更することをお勧めします。
you will get exact result::
output // 28 years 7 months 7 days
function getAge(dateVal) {
var
birthday = new Date(dateVal.value),
today = new Date(),
ageInMilliseconds = new Date(today - birthday),
years = ageInMilliseconds / (24 * 60 * 60 * 1000 * 365.25 ),
months = 12 * (years % 1),
days = Math.floor(30 * (months % 1));
return Math.floor(years) + ' years ' + Math.floor(months) + ' months ' + days + ' days';
}