[PHP] resetting a variable at 12am

De Code

Newbie
Joined
May 14, 2018
Messages
12
Reaction score
0
Hello,

I'm trying to make a variable that resets every 12 am

how can i do that?
 
if( $date->format( 'H') == 0 && $date->format( 'i') == 0) {
# Reset your variable here
}
do they need to be declared somehow? integrate time into the script or takes it from the server?
 
You can set up a cron job to run once per day at 12am. Check out https://www.cloudways.com/blog/schedule-cron-jobs-in-php/
 
I... I don't understand what it is you're trying to do.

A variable in PHP is only going to exist for as long as the script is running.


E.G.

PHP:
<?php

$var = "foobar";

// Some code

print($var);

?>

When you call that script $var will only be set to "foobar" until the script finishes. You can't then change the value of it somewhere and it will be a different value.

Even when building plugins in things like Wordpress there is still a script lifecycle. It sounds like you want to be reading something in from your filesystem at midnight every day, or want a script to react differently if it's run on two different days.

Either way, I think your approach is probably wrong.

You can set up a cron job to run once per day at 12am. Check out https://www.cloudways.com/blog/schedule-cron-jobs-in-php/

Will run your script once a day at midnight, which might be exactly what you're after.
 
I... I don't understand what it is you're trying to do.

A variable in PHP is only going to exist for as long as the script is running.


E.G.

PHP:
<?php

$var = "foobar";

// Some code

print($var);

?>

When you call that script $var will only be set to "foobar" until the script finishes. You can't then change the value of it somewhere and it will be a different value.

Even when building plugins in things like Wordpress there is still a script lifecycle. It sounds like you want to be reading something in from your filesystem at midnight every day, or want a script to react differently if it's run on two different days.

Either way, I think your approach is probably wrong.



Will run your script once a day at midnight, which might be exactly what you're after.


cumulative variable and i need to reset it to default for each member at mednight
 
Last edited:
That still doesn't answer the question. Where is that variable getting stored? Is it in a database?
sorry, yeah its stored in Mysql Database, localhost phpmyadmin

its food log script calculates calories ate per day, so i need it to reset at 12 midnight
 
sorry, yeah its stored in Mysql Database, localhost phpmyadmin

its food log script calculates calories ate per day, so i need it to reset at 12 midnight

You want to keep that information stored in the DB along with a date. Just check the most recent row in the DB for the user, and then if the dates are different, insert a new row (e.g. start from 0). Otherwise update the existing row for the current day.
 
Back
Top