Countdown to sunday - php

I am currently looking at creating a script for my site that will count down to sunday of that week, every week.
Example:
The user visits the site on a saturday at 11:30am, they will be greeted with:
"The next time this page will be updated is in 0 days, 12 hours and 30 minutes."
Any ideas?

You can use this little trick to get a timestamp for midnight next Sunday:
$sunday = strtotime('next Sunday');
See this answer for how to format it into something useful. Right now I get this:
print_r(dateDifference($sunday, time()));
Array
(
[years] => 0
[months_total] => 0
[months] => 0
[days_total] => 0
[days] => 0
[hours_total] => 4
[hours] => 4
[minutes_total] => 256
[minutes] => 16
[seconds_total] => 15387
[seconds] => 27
)

I am using similar to this solution in one of my pojects. You can use it like this:
ago(strtotime("next sunday")) but you need to change $difference = $now - $time; to $difference = $time - $now;

Here's one solution:
http://jsfiddle.net/foxbunny/xBE7L/
It also automatically updates every second.
Edit: I've included the offset parameter, and you use it to supply the difference between user's and server's time-zone if necessary.

Related

How to calculate time difference between SQL time and PHP time

I have set times in SQL in this format: 2016-01-03 12:13:26.
I would like to calculate the number of hours and minutes (if hours<1) going from NOW() to that particular SQL time.
I've been looking at all the different threads here but I can't seem to grasp how to convert PHP different time formats to SQL's.
This is the code I've been using, but this will only give me back hours up to 12, and minutes also. Don't know how to use it with days.
$now = date("d/m/Y h:i:s");
$commentime = strtotime($SQLTIME);
$timetocomment = $now - $commentime;
For instance, this code will yield "12 hours ago" for data I posted 24 hours ago to SQL.
How can I do it? Thank you.
This is my suggestion to use date() in this format date("Y-m-d h:i:s"). Than you will get the complete difference in an array.
function dateDifference($date_1 ,$date_2)
{
$datetime1 = date_create($date_1);
$datetime2 = date_create($date_2);
$interval = date_diff($datetime1, $datetime2);
return $interval;
}
$now = date("Y-m-d h:i:s");
$sqlTime = "2016-01-03 12:13:26";
$DateDiffArr = dateDifference($now,$sqlTime);
echo "<pre>";
print_r($DateDiffArr);
Result Is:
DateInterval Object
(
[y] => 0
[m] => 0
[d] => 0
[h] => 22
[i] => 45
[s] => 55
[weekday] => 0
[weekday_behavior] => 0
[first_last_day_of] => 0
[invert] => 1
[days] => 0
[special_type] => 0
[special_amount] => 0
[have_weekday_relative] => 0
[have_special_relative] => 0
)
In resultant array, you can get the all difference as you need like in years, months, days, minutes, seconds etc.

What is the best way to subtract "after-hours" time from a date range to get an accurate "active" time?

I have 2 parallel arrays that contain mysql timestamps. The first array contains "Start" times, and the second array contains "Stop" times.
Example
Starts
Array ( [0] => 2014-12-05 12:21:29 [1] => 2014-12-10 07:14:17 [2] => 2014-12-10 12:43:47 [3] => 2014-12-12 07:39:28 [4] => 2014-12-12 08:13:30 )
Stops
Array ( [0] => 2014-12-08 08:08:37 [1] => 2014-12-10 10:13:37 [2] => 2014-12-12 07:18:53 [3] => 2014-12-12 08:10:39 [4] => 2014-12-12 08:27:26 )
I need to add the total times based off of all the starts and stops in each array, but I also need to subtract all the time that was "after-hours". This is for reporting purposes.
So let's say that any time after 4pm(16:00) and before 6am(06:00) needs to be removed from the total active time that will be reported.
I already have it subtracting weekend time, but I also need it to remove the "after-hours" time if it occurs after-hours on a weekday. The checks for weekday/weekend aren't a problem, but figuring out how to check if the time range contains hours that were after business hours is what is stumping me.
Here is how I am subtracting the weekend time:
function getWeekendSeconds($starts, $stops){
$count = 0;
$secondsToSubtract = 0;
$secondsInDay = 86400;
if(count($starts) > count($stops)){
while(count($starts) != count($stops)){
array_pop($starts);
}
}
foreach($starts as $start){
$stop = $stops[$count];
$startTime = strtotime($start);
$stopTime = strtotime($stop);
while($startTime < $stopTime){
$dayNum = date('N', strtotime(date("Y-m-d H:i:s", $startTime)));
if($dayNum == 6 or $dayNum == 7){
$secondsToSubtract += $secondsInDay;
}
$startTime = strtotime(date("Y-m-d H:i:s", $startTime) . " +1 day");
}
$count = $count + 1;
}
return $secondsToSubtract;
}
I was thinking of doing something similar to calculate the after-hours time, but I'm having trouble wrapping my head around it.
Would I just take the start time and add time to it and check it each time to see if it's greater than the business closing time? Is it simpler than I'm making it? Is there a mysql solution I'm not aware of?
Thanks in advance!

PHP - Converting Seconds to Minutes discrepancy

I'm running into a discrepancy with either my conversion of an integer into defined Minutes.
<?php
$seconds = 269;
echo date("G:i:s", $seconds);
// result: 0:04:29
?>
I figured I'd double check on some sites to see if the conversion is correct:
Here's one I found:
http://www.thecalculatorsite.com/conversions/time.php
The result returned is: 4.4833333333333
Example 1 returns: 0:04:29
Example 2 returns: 4.4833333333333
I'm confused about this.
What am I missing here. Am I using the date() function incorrectly?
Be careful with date(). it expects to be provided with a PHP timestamp, which is number of seconds since midnight, Jan 1/1970. It will "work" for small timestamp values, but will get increasingly wrong as you pass in "larger" timestamps, as you will be dealing with months/days/years in 1970, plus leapyears, etc...
As for the conversion, what's wrong with it? 4.48333... is 4 full minutes, and 0.483333 is simply 29/60.
You can use DateTime class for time calculation:
Code:
$start = new DateTime;
$end = clone $start;
$end->modify('+269 seconds');
$diff = $start->diff($end);
print_r($diff);
Output:
DateInterval Object
(
[y] => 0
[m] => 0
[d] => 0
[h] => 0
[i] => 4
[s] => 29
[invert] => 0
[days] => 0
)
As you can see in output, you have all the info you need. To access it, just use $diff->i for minutes, $diff->s for seconds etc.

PHP get time period by timestamp

How can I get the time period (day, week, month) by a given timestamp? I do not want the date. I want the next time period based on the amount of seconds.
Is there a PHP native function for that?
Example:
$period = getTimeperiod( 15638400 );
My attempt: I could check and count the seconds:
if x <= 60 => min
if x <= 60*60 => hour
if x <= 60*60*24 => day
...
Edit:
Time period means either minute, hour, day, week, ... as stated above... ?! So all I want is the corresponding time period for a timestamp.
Example: (day = 86400 secs) then a timestamp with getTimeperiod( 85000 ) should be "day".
I think you're searching for something like the class DateInterval ...
It is part of PHP since 5.3.0 and has a static function called createFromDateString() where you can set up a DateInterval from a string like "3600 seconds". From this object you then can get the day, month, year and so on.
Take a look at this page:
http://www.php.net/manual/de/dateinterval.createfromdatestring.php
But is this on the right path returning an interval object (period)? Cred to #SimonSimCity for pointing out DateInterval. If you guide me I could improve the answer.
<?php
$timestamp = 15638400;
echo "The timestamp $timestamp is " . date("Y-m-d H:i:s", 15638400) . "<br \>";
echo "<pre>";
print_r (DateInterval::createFromDateString(date("Y \\y\\e\\a\\r\\s m \\m\\o\\n\\t\\h\\s d \\d\\a\\y\\s\\ H \\h\\o\\u\\r\\s i \\m\\i\\n\\u\\t\\e\\s s \\s\\e\\c\\o\\n\\d\\s", 15638400 ) ) );
echo "</pre>"
?>
Outputting
The timestamp 15638400 is 1970-07-01 00:00:00
DateInterval Object
(
[y] => 1970
[m] => 7
[d] => 1
[h] => 0
[i] => 0
[s] => 0
[invert] => 0
[days] => 0
)
I solved it that way:
/*
seconds 0
minutes 1
hours 2
days 3
week 4
month 5
year 6
decade 7
century 8
millenium 9
*/
$arTimes = array(
0 => 1,
1 => 60,
2 => 60*60,
3 => 60*60*24,
4 => 60*60*24*7,
5 => 60*60*24*7*4,
6 => 60*60*24*7*4*12,
7 => 60*60*24*7*4*12*10,
8 => 60*60*24*7*4*12*10*10,
9 => 60*60*24*7*4*12*10*10*10
);
$nDiff = strtotime( $nTo ) - strtotime( $nFrom );
switch( $nDiff )
{
// check difference and time period
case $nDiff <= $arTimes[ 1 ]:
$nGranularity = 0;
break;
...
}

filtering by date php for this month and last x days and this week

I have a date that returns in a string as 2012-03-19 05:00:32, its not coming from the database
I can use below to search for the last 30 days
$date = '2012-03-19 05:00:32';
if (strtotime($date) >= strtotime('-7 days')) {
// do something
}
Problem is if today is the 19th March, i was to search from the 11th to the 18th for the last 7 days and that seems to search for the last 7 days by calculating 24 hours * 7 by my searches need to start from 00:00:01 each day.
My plan is to break the date down into Year, Month and Day then check if year = 12, then check if month = 3, then check if date between 11 and 18.
Im just wondering if there is a more efficient way to do this or if im on the right track.
I also have the same issue with running a search on all info from this month and also want to search for all info this week starting on Monday.
So this is just asking if my method is sound or if there is a more efficient method.
$mytime = new DateTime('2012-03-19 05:00:32');
$mydate = new DateTime($mytime->format('Y-m-d')); //keep date only, exclude the time component
$now=new DateTime; //includes hours, minutes, seconds
$today=new DateTime($now->format('Y-m-d')); //time set to 0:00
$interval = $mydate->diff($today);
if($interval->format('d') <=7) { //assuming that $mydate isn't in the past
//do something
}
I'd suggest to use DateTime class ...
<?php
$d1=new DateTime("2012-07-08 11:14:15.638276");
$d2=new DateTime("2012-07-08 11:14:15.889342");
$diff=$d2->diff($d1);
print_r( $diff ) ;
/* returns:
DateInterval Object
(
[y] => 0
[m] => 0
[d] => 0
[h] => 0
[i] => 0
[s] => 0
[invert] => 0
[days] => 0
)
*/
?>

Categories