2017-09-02 10 views
1

です。作業時間の計算に問題があります。 この場合、文字列として開始する時刻から始めます($ time)。 次に、時刻を00:00:00に変更し、その結果を新しい変数($ newtime)として保存します。 次に、$ timeと$ newtimeの違いを計算する必要がありますが、完全に理解できない書式設定の問題があります。助けてくれますか?時間計算の問題があります。フォーマットは

$time = "2017-09-01 11:00:00"; //must start as a string like this 
$newtime = new DateTime($time); 
$newtime->setTime(00, 00,00); //change time to 00:00:00 
$worktime = round((strtotime($time) - $newtime)/3600, 2); 
echo "Hours Worked: " . $worktime . "<br>"; 
+0

なぜ '00までの時間を設定して00? – Dekel

+0

タイプ8 - DateTimeクラスのオブジェクトをintに変換できませんでした。 – z3nth10n

+0

はい、まさにそのエラー「タイプ8」 –

答えて

3

あなたはDateTimeオブジェクトとタイムスタンプを引いているので、それはそれはできない、intDateTimeオブジェクトを変換しようとします。あなたは(int型にオブジェクトをキャストしようとしている)の種類を混合している

<?php 
$time = "2017-09-01 11:00:00"; //must start as a string like this 
$newtime = new DateTime($time); 
$newtime->setTime(00, 00,00); //change time to 00:00:00 
$worktime = round((strtotime($time) - $newtime->getTimestamp())/3600, 2); // notice the $newtime->getTimestamp() call 
echo "Hours Worked: " . $worktime . "<br>"; 
+0

私はそのヘルプ 'ishegg'のために十分にあなたに感謝できません!完璧に動作し、私はそれが大好きです。 –

+0

私はお手伝いします!あなたの質問が解決された場合は、そのように印を付けるために最も役立つ答えを受け入れてください。 – ishegg

1

..:あなたは、2つのintを減算し、DateTimeオブジェクトのためのタイムスタンプを取得する必要がありますあなたがエラーを無効にしているためにあなたが行っているエラーについては気がつかなかったかもしれません。

http://php.net/manual/es/datetime.gettimestamp.php

あなたは両方の方法でそれを行うことができます:

、DateTimeクラスがあなたにもたらす方法を使用してください

$newtime->getTimestamp()

または本使用して:

date_timestamp_get($newtime)

このよう

$time = "2017-09-01 11:00:00"; //must start as a string like this 
$newtime = new DateTime($time); 
$newtime->setTime(00, 00,00); //change time to 00:00:00 
$worktime = round((strtotime($time) - date_timestamp_get($newtime))/3600, 2); 
echo "Hours Worked: " . $worktime . "<br>"; 

してください、これを使用しての自由である:00:http://sandbox.onlinephpfunctions.com/code/f78c993f709a67ac2770d78bb809e68e3a679707

関連する問題