私はトップ30の音楽チャートを作成しています。
2つのmysql形式の日時の間の週数を取得できますか?お気に入り
から:2013-01-15 11:41:14
現在の日時:2013-02-25 13:41:14
SQL を使用する方が優れていますが、php では次のようになります。
<?php
$datetime1 = new DateTime('2013-01-15 11:41:14');
$datetime2 = new DateTime('2013-02-25 13:41:14');
$interval = $datetime1->diff($datetime2);
$diff = $interval->format('%d');
echo (int)$diff/7;
mysql で DATEDIFF(date1, date2) 関数を試してください。DATEDIFF() は、指定された 2 つの日付の間の日付を返します。出力から簡単に週を取得できることを願っています。
メソッドを使用しstrtotime()
て、現在の時刻と MySQL データベースにある時刻の差をカウントできます。
を使用して時間を減算しますstrtotime
$difference = strtotime($first_date)-strtotime($second_date);
$weeks = round($difference / 604800 );
<?php
$db_time_a = strtotime('2013-01-15 11:41:14');
$db_time_b = strtotime('2013-02-25 13:41:14');
$seconds_in_between = $db_time_a - $db_time_b;
$hours = (int)($seconds_in_between/60/60);
$minutes = (int)($seconds_in_between/60)-$hours*60;
$seconds = (int)$seconds_in_between-$hours*60*60-$minutes*60;
echo 'The time in seconds in between the two times is: '.$seconds_in_between.' (Hours:'.$hours.', Minutes:'.$minutes.', Seconds:'.$seconds.')';
?>
「remainder」を使用して秒を細かく分割し、必要な時間距離に到達するまで、分 (および残りの秒) などを調べることができます。
関数はWEEK()
クエリに役立ちますか?
私はMysqlをよく知りませんが、次のことを前提としています:
select WEEK(date1-date2) from mytable where name='Ken';