Make cookie expire after 3 or 5 seconds - php

I need a cookie on my site just for a few seconds. What i tried is this:
$value = 12;
$cookie = setcookie("myCookie", $value, time() + 3);
The cookie gets created, but needs longer to expire than 3 seconds

Very simple time math here.
$value = 12;
$seconds_in_seconds = 1;
$minute_in_seconds = 60
$hour_in_seconds = 60 * 60;
$day_in_seconds = 60 * 60 * 24
$week_in_seconds = 60 * 60 * 24 * 7
/*ect...*/
/*60 seconds in a minute. Times 2 minutes plus 10 seconds. Makes 2 minutes and ten seconds.*/
$time = time() + (2 * $minute_in_seconds) + (10 * $seconds)
$cookie = setcookie("myCookie", $value, $time);

Related

Do action once but only repeat it after x time

So basically, i need to notify a user if:
a) sensor disconnects and there hasn't been an alarm sent in last $send_threshold
b) if alarm sent on same day and greater than $repeat_threshold.
Example values of the variables
$send_threshold = 12 * 60; // 12 min
$repeat_threshold = 2 * 60 * 60 + 45 * 60; // 2 hr 45 min
I'm drawing a complete blank on how to make it happen. The sensors are stateless so there is no way for me to check if a sensor is online, other than to check timestamps of received data that the sensor posted to the api.
/* FUNCTIONS */
function handleDisconnectAlerts($sensor,$dataset,$users,$settings)
{
end($dataset);
$last_timestamp = $dataset[key($dataset)]['timestamp'];
$now = time();
if($now > $last_timestamp && $now - $last_timestamp > $settings['disconnect_alarm'] * 60)
{
$send_threshold = $settings['disconnect_alarm'] * 60;
$repeat_threshold = $settings['disconnect_alarm_repeat_hours'] * 60 * 60 + $settings['disconnect_alarm_repeat_minutes'] * 60;
//not really sure what to do from here.
}
}
Well, i figured it out myself i guess. The initial check covers the intial interval of 60 * $settings['disconnect_alarm']. From there i just needed to check that an alarm hasn't been sent in $repeat_threshold time. Works like a charm.
function handleDisconnectAlerts($sensor,$dataset,$users,$settings)
{
end($dataset);
$last_timestamp = $dataset[key($dataset)]['timestamp'];
$now = time();
if($now > $last_timestamp && $now - $last_timestamp > $settings['disconnect_alarm'] * 60)
{
$repeat_threshold = $settings['disconnect_alarm_repeat_hours'] * 60 * 60 + $settings['disconnect_alarm_repeat_minutes'] * 60;
$n_sent = R::count('sensoralerts',' timestamp >=:time and field_name="disconnect" and sensor_id=:id ',
[':time'=>$repeat_threshold,':id'=>$sensor['id']]
);
if($n_sent == 0){
multiSendDisconnect($users,$sensor);
}
}
}

Most efficient way to get seconds left in this hour

I need to get the remaining seconds in the actual hour for caching weather-data. What's the most efficient method of doing that?
Simply use PHP's time function as follows:
<?php
function getSecondsRemaining()
{
//Take the current time in seconds since the epoch, modulus by 3600 to get seconds in the hour, then subtract this from 3600 (# secs in an hour)
return 3600 - time() % (60*60);
}
?>
This should get you the performance you want.
Here you go mate:
$currentMinute = date("i");
$minuteLeft = 60 - $currentMinute;
$secondLeft = $minuteLeft * 60;
$secondLeft += date("s");
echo $secondLeft;
How about
$date = new DateTime("now");
$seconds_left = ( ( 59 - $date->format("i") ) * 60 ) + ( 60 - $date->format("s") );

I am new to php and I need help understanding this code

$ep1 = mktime(19,32,56,5,10,1965);
$ep2 = mktime(4,29,11,11,20,1962);
echo($ep2);
$diff_seconds = $ep1 - $ep2;
$diff_weeks = floor($diff_seconds/604800); // 128
*$diff_seconds -= $diff_weeks * 604800;
$diff_days = floor($diff_seconds/86400);
*$diff_seconds -= $diff_days * 86400;
$diff_hrs = floor($diff_seconds/3600);
*$diff_seconds -= $diff_hrs * 3600;
$diff_mins = floor($diff_seconds/60);
*$diff_seconds -= $diff_mins * 60;
echo('<br>');
echo("Difference = $diff_weeks,$diff_days,$diff_hrs,$diff_mins");
I want to understand why the lines marked with an asterisks(*) has been done?
Thank you
Let's start the other way round.
Say you want to express the timespan (3 days) + (17 hours) + (47 minutes) + 13 seconds in seconds. That would be (without leap seconds et al)
(3*86400) + (17*3600) + (47*60) + 13
= 259200 + 61200 + 2820 13
= 323233 (=$diff_seconds)
Now let's take a look what the script does ( we skip the weeks and start with the days)
$diff_days = floor($diff_seconds/86400);
= floor(323233/86400);
= floor(3,7...);
= 3
$diff_seconds -= $diff_days * 86400;
$diff_days * 86400 = 259200 <-- see? That's the first factor in (3*86400) + (17*3600) + (47*60) + 13
those 259200 have already "been handled", so they are subtracted from the the seconds that are still to be processed. After that $diff_seconds only counts the seconds that can't be expressed as days (as integer).
The next step handles all seconds that can be expressed as (integer) hours and again subtracts them from the remaining seconds and so on and on.
Those are Assignment operators, here is a list of all operators in php.

Date Difference in days in php

I have a function to calculate the difference between two dates.
function getDateDifference($to, $from, $in) {
$diff = abs($to - $from);
$years = floor($diff / (365 * 60 * 60 * 24));
$months = floor(($diff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
$days = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));
if ($in == "days") {
return $days;
} else if ($in == "months") {
return $months;
} else if ($in == "years") {
return $years;
}
}
For the parameters i first convert the two dates into seconds like this,
checkin = '2012-07-26';
checkout = '2012-07-27';
check_in_date = strtotime(checkin);
check_out_date = strtotime(checkout);
im getting the correct difference when it comes to difference less than one month. But if the difference is more than one month, im always getting the difference as 1. Can someone tell me wat the problem is.
Currently, a month is always 30 * 60 * 60 * 24 sec, aka 30 days.
Your problem is that we're in July, and there are 31 days, not 30. You must take care of number of days per month.
You can make use of the DateTime class.
http://php.net/manual/en/datetime.diff.php
$checkin = new DateTime("2012-07-23");
$checkout = new DateTime("2012-07-27");
$difference = $checkin->diff($checkout);
echo "differrence = " . $difference->format('%R%a days');

timestamp countdown in javascript and php

I need some help to code a script that counts down the days, hours, minutes and seconds from a unix timestamp.
timestamp is created with php
<? php echo hours ();?>
And I want the script to count down in real time with JavaScript.
Example
2 days, 3:15:39
Hope someone can help me a little:)
First you have some PHP errors. It should be e.g.
<?php echo time(); ?>
I'm using a timestamp in JavaScript for the ease of showing an example.
http://jsfiddle.net/pimvdb/AvXd3/
// the difference timestamp
var timestamp = (Date.now() + 1000 * 60 * 60 * 24 * 3) - Date.now();
timestamp /= 1000; // from ms to seconds
function component(x, v) {
return Math.floor(x / v);
}
var $div = $('div');
setInterval(function() { // execute code each second
timestamp--; // decrement timestamp with one second each second
var days = component(timestamp, 24 * 60 * 60), // calculate days from timestamp
hours = component(timestamp, 60 * 60) % 24, // hours
minutes = component(timestamp, 60) % 60, // minutes
seconds = component(timestamp, 1) % 60; // seconds
$div.html(days + " days, " + hours + ":" + minutes + ":" + seconds); // display
}, 1000); // interval each second = 1000 ms

Categories