Total of all values in a day (24h)










-3















I'd like to add all values in a database from the last 24 hours. Each value has its TimeStamp in the database.



I tried to do that with a for-loop, which adds 86400 secs (24h) every time, picks all values from one day and after that it adds all values.



Heres my code:



> `$datestart = 153839251200; //start date

for($uts = $uts; $uts > $datestart; $datestart + 86400)

if (($uts <= ($datestart + 86400)) && ($uts > $datestart))

$uts = $datestart + 86400;

$valueFinal = $valueFinal + $value;




if($Zeitalt != $uts)

$Zeitalt=date('l, F j y H:i:s',$uts);
$uts *= 1000; // convert from Unix timestamp to JavaScript time
$data = array((float)$uts,(float) $valueFinal);


`


I hope this explanation is enough, I'm not English speaking as much, otherwise just ask for more information.



Regards DR.Alfred










share|improve this question

















  • 1





    What is the question ? You have given us PHP code; with no details about your table structure and some relevant sample data.

    – Madhur Bhaiya
    Nov 14 '18 at 14:12











  • stackoverflow.com/a/1888593/522479 could be interesting.

    – Cobra_Fast
    Nov 14 '18 at 14:27















-3















I'd like to add all values in a database from the last 24 hours. Each value has its TimeStamp in the database.



I tried to do that with a for-loop, which adds 86400 secs (24h) every time, picks all values from one day and after that it adds all values.



Heres my code:



> `$datestart = 153839251200; //start date

for($uts = $uts; $uts > $datestart; $datestart + 86400)

if (($uts <= ($datestart + 86400)) && ($uts > $datestart))

$uts = $datestart + 86400;

$valueFinal = $valueFinal + $value;




if($Zeitalt != $uts)

$Zeitalt=date('l, F j y H:i:s',$uts);
$uts *= 1000; // convert from Unix timestamp to JavaScript time
$data = array((float)$uts,(float) $valueFinal);


`


I hope this explanation is enough, I'm not English speaking as much, otherwise just ask for more information.



Regards DR.Alfred










share|improve this question

















  • 1





    What is the question ? You have given us PHP code; with no details about your table structure and some relevant sample data.

    – Madhur Bhaiya
    Nov 14 '18 at 14:12











  • stackoverflow.com/a/1888593/522479 could be interesting.

    – Cobra_Fast
    Nov 14 '18 at 14:27













-3












-3








-3








I'd like to add all values in a database from the last 24 hours. Each value has its TimeStamp in the database.



I tried to do that with a for-loop, which adds 86400 secs (24h) every time, picks all values from one day and after that it adds all values.



Heres my code:



> `$datestart = 153839251200; //start date

for($uts = $uts; $uts > $datestart; $datestart + 86400)

if (($uts <= ($datestart + 86400)) && ($uts > $datestart))

$uts = $datestart + 86400;

$valueFinal = $valueFinal + $value;




if($Zeitalt != $uts)

$Zeitalt=date('l, F j y H:i:s',$uts);
$uts *= 1000; // convert from Unix timestamp to JavaScript time
$data = array((float)$uts,(float) $valueFinal);


`


I hope this explanation is enough, I'm not English speaking as much, otherwise just ask for more information.



Regards DR.Alfred










share|improve this question














I'd like to add all values in a database from the last 24 hours. Each value has its TimeStamp in the database.



I tried to do that with a for-loop, which adds 86400 secs (24h) every time, picks all values from one day and after that it adds all values.



Heres my code:



> `$datestart = 153839251200; //start date

for($uts = $uts; $uts > $datestart; $datestart + 86400)

if (($uts <= ($datestart + 86400)) && ($uts > $datestart))

$uts = $datestart + 86400;

$valueFinal = $valueFinal + $value;




if($Zeitalt != $uts)

$Zeitalt=date('l, F j y H:i:s',$uts);
$uts *= 1000; // convert from Unix timestamp to JavaScript time
$data = array((float)$uts,(float) $valueFinal);


`


I hope this explanation is enough, I'm not English speaking as much, otherwise just ask for more information.



Regards DR.Alfred







mysql sql






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 14:11









DR.AlfredDR.Alfred

11




11







  • 1





    What is the question ? You have given us PHP code; with no details about your table structure and some relevant sample data.

    – Madhur Bhaiya
    Nov 14 '18 at 14:12











  • stackoverflow.com/a/1888593/522479 could be interesting.

    – Cobra_Fast
    Nov 14 '18 at 14:27












  • 1





    What is the question ? You have given us PHP code; with no details about your table structure and some relevant sample data.

    – Madhur Bhaiya
    Nov 14 '18 at 14:12











  • stackoverflow.com/a/1888593/522479 could be interesting.

    – Cobra_Fast
    Nov 14 '18 at 14:27







1




1





What is the question ? You have given us PHP code; with no details about your table structure and some relevant sample data.

– Madhur Bhaiya
Nov 14 '18 at 14:12





What is the question ? You have given us PHP code; with no details about your table structure and some relevant sample data.

– Madhur Bhaiya
Nov 14 '18 at 14:12













stackoverflow.com/a/1888593/522479 could be interesting.

– Cobra_Fast
Nov 14 '18 at 14:27





stackoverflow.com/a/1888593/522479 could be interesting.

– Cobra_Fast
Nov 14 '18 at 14:27












1 Answer
1






active

oldest

votes


















0














You've tagged this as SQL so I'm assuming an answer in SQL is what you're looking for.



Firstly, I'm not sure why you're going to the effort of using a loop to get the total when SQL has the SUM function.



I'll give you this in T-SQL as I'm not familiar with MySQL but it shouldn't be too difficult to change it to MySQL:



SELECT 
SUM(YourValue)
FROM
YourTable
WHERE
YourTimeStamp > getdate() - 1


I think the MySQL equivalent of GETDATE() is NOW().






share|improve this answer























  • I tried it like this: $result = mysql_query("SELECT SUM($total) * FROM Messwerte WHERE $uts >= NOW() - INTERNAL 1 DAY"); //$uts: TimeStamp

    – DR.Alfred
    Nov 15 '18 at 6:51












  • So what happened when you ran this? I'm not an expert on MySQL but I wouldn't think you would need the * in the select. Also, you should be using INTERVAL, not INTERNAL.

    – codemac
    Nov 15 '18 at 16:59











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302196%2ftotal-of-all-values-in-a-day-24h%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You've tagged this as SQL so I'm assuming an answer in SQL is what you're looking for.



Firstly, I'm not sure why you're going to the effort of using a loop to get the total when SQL has the SUM function.



I'll give you this in T-SQL as I'm not familiar with MySQL but it shouldn't be too difficult to change it to MySQL:



SELECT 
SUM(YourValue)
FROM
YourTable
WHERE
YourTimeStamp > getdate() - 1


I think the MySQL equivalent of GETDATE() is NOW().






share|improve this answer























  • I tried it like this: $result = mysql_query("SELECT SUM($total) * FROM Messwerte WHERE $uts >= NOW() - INTERNAL 1 DAY"); //$uts: TimeStamp

    – DR.Alfred
    Nov 15 '18 at 6:51












  • So what happened when you ran this? I'm not an expert on MySQL but I wouldn't think you would need the * in the select. Also, you should be using INTERVAL, not INTERNAL.

    – codemac
    Nov 15 '18 at 16:59
















0














You've tagged this as SQL so I'm assuming an answer in SQL is what you're looking for.



Firstly, I'm not sure why you're going to the effort of using a loop to get the total when SQL has the SUM function.



I'll give you this in T-SQL as I'm not familiar with MySQL but it shouldn't be too difficult to change it to MySQL:



SELECT 
SUM(YourValue)
FROM
YourTable
WHERE
YourTimeStamp > getdate() - 1


I think the MySQL equivalent of GETDATE() is NOW().






share|improve this answer























  • I tried it like this: $result = mysql_query("SELECT SUM($total) * FROM Messwerte WHERE $uts >= NOW() - INTERNAL 1 DAY"); //$uts: TimeStamp

    – DR.Alfred
    Nov 15 '18 at 6:51












  • So what happened when you ran this? I'm not an expert on MySQL but I wouldn't think you would need the * in the select. Also, you should be using INTERVAL, not INTERNAL.

    – codemac
    Nov 15 '18 at 16:59














0












0








0







You've tagged this as SQL so I'm assuming an answer in SQL is what you're looking for.



Firstly, I'm not sure why you're going to the effort of using a loop to get the total when SQL has the SUM function.



I'll give you this in T-SQL as I'm not familiar with MySQL but it shouldn't be too difficult to change it to MySQL:



SELECT 
SUM(YourValue)
FROM
YourTable
WHERE
YourTimeStamp > getdate() - 1


I think the MySQL equivalent of GETDATE() is NOW().






share|improve this answer













You've tagged this as SQL so I'm assuming an answer in SQL is what you're looking for.



Firstly, I'm not sure why you're going to the effort of using a loop to get the total when SQL has the SUM function.



I'll give you this in T-SQL as I'm not familiar with MySQL but it shouldn't be too difficult to change it to MySQL:



SELECT 
SUM(YourValue)
FROM
YourTable
WHERE
YourTimeStamp > getdate() - 1


I think the MySQL equivalent of GETDATE() is NOW().







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 17:07









codemaccodemac

264




264












  • I tried it like this: $result = mysql_query("SELECT SUM($total) * FROM Messwerte WHERE $uts >= NOW() - INTERNAL 1 DAY"); //$uts: TimeStamp

    – DR.Alfred
    Nov 15 '18 at 6:51












  • So what happened when you ran this? I'm not an expert on MySQL but I wouldn't think you would need the * in the select. Also, you should be using INTERVAL, not INTERNAL.

    – codemac
    Nov 15 '18 at 16:59


















  • I tried it like this: $result = mysql_query("SELECT SUM($total) * FROM Messwerte WHERE $uts >= NOW() - INTERNAL 1 DAY"); //$uts: TimeStamp

    – DR.Alfred
    Nov 15 '18 at 6:51












  • So what happened when you ran this? I'm not an expert on MySQL but I wouldn't think you would need the * in the select. Also, you should be using INTERVAL, not INTERNAL.

    – codemac
    Nov 15 '18 at 16:59

















I tried it like this: $result = mysql_query("SELECT SUM($total) * FROM Messwerte WHERE $uts >= NOW() - INTERNAL 1 DAY"); //$uts: TimeStamp

– DR.Alfred
Nov 15 '18 at 6:51






I tried it like this: $result = mysql_query("SELECT SUM($total) * FROM Messwerte WHERE $uts >= NOW() - INTERNAL 1 DAY"); //$uts: TimeStamp

– DR.Alfred
Nov 15 '18 at 6:51














So what happened when you ran this? I'm not an expert on MySQL but I wouldn't think you would need the * in the select. Also, you should be using INTERVAL, not INTERNAL.

– codemac
Nov 15 '18 at 16:59






So what happened when you ran this? I'm not an expert on MySQL but I wouldn't think you would need the * in the select. Also, you should be using INTERVAL, not INTERNAL.

– codemac
Nov 15 '18 at 16:59




















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302196%2ftotal-of-all-values-in-a-day-24h%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3