im in the middle editting this js while it refering to this lines as data. i tried to edit the strtotime part but nothing is happening. tried to adding hours or minutes it still displays the same. how do i edit the result time ?
function dav_clock_time( $return_data = false ) {
$now = strtotime( "now +4 hours 2 minutes" );
$next_tuesday = strtotime( "next Tuesday +4 hours 2 minutes" );
$time_dif = $next_tuesday - $now;
if ( $time_dif <= 0 ) {
$next_tuesday = $next_tuesday + 604800; // 1 week = 604800
}
$data_end_actions = date( 'Y/m/d', $next_tuesday );
if ( $return_data ) {
return $data_end_actions;
}
echo $data_end_actions;
}
Related
There is a WordPress and ACF field with a date in the format 2022-04-30. From this date need to calculate 2 other dates, -2 days and +25 days. The problem is that it is necessary to take into account only working days, i.e. check weekends and holidays.
For example, we set the date May 3, 2022, which is Tuesday. From this date, I need to subtract 2 days, i.e. May 1, 2022, but this is Sunday, so we have to return to the first working day before May 1, i.e. Friday April 29, 2022. It's the same with holidays.
At the moment I have this code:
$setDate = get_field('set_date'); // ACF field, set May 3, 2022 (2022-05-03)
$offDate = wp_date('j F Y', strtotime('-2 days', strtotime($setDate)));
echo $offDate; // returns Sunday, May 1, 2022
I found holidays and weekends in json https://github.com/d10xa/holidays-calendar/blob/master/json/consultant2022.json
So I need to compare the given date with the dates from json and if there is a match, then minus one day and check the received date again. If there is a match, again minus one day and so on until no matches are found. Am I thinking correctly and can you tell me how to implement it? I am a very bad programmer, but there is a task)
At the moment I was only able to compare the dates and return the result found/not found. But I can't figure out how to take days off on the basis of this and send it for verification again(
$setDate = '2022-05-01';
$file = file_get_contents('https://raw.githubusercontent.com/d10xa/holidays-calendar/master/json/consultant2022.json', true);
$data = json_decode($file);
$found = array_search($setDate, $data->holidays);
if ($found === False) {
echo 'Not Found';
} else {
echo 'found';
}
The following has been tested on a few dates and I think it works as it should.
/*
JSON file is saved locally to a sub-directory
for the current working script directory.
This is to avoid unneccessary hits to the
remote site.
*/
$format='Y-m-d';
$url='https://raw.githubusercontent.com/d10xa/holidays-calendar/master/json/consultant2022.json';
$setDate = '2022-05-01';
$filepath=sprintf('%s/json/%s', __DIR__, basename( $url ) );
if( !file_exists( $filepath ) ){
file_put_contents( $filepath, file_get_contents( $url ) );
}
# read the file and generate JSON
$json=json_decode( file_get_contents( $filepath ) );
$hols=$json->holidays;
# create the initial DateTime object and find which weekday we are dealing with
# where 1 (for Monday) through 7 (for Sunday)
$obj=new DateTime( $setDate );
$date=$obj->format( $format );
$day=$obj->format('N');
# Is the given date a holiday/weekend?
if( array_search( $date, $hols ) ){
if( $day > 5 ){
# Date is either Sat or Sun... go back to previous working day
$subtract = 2 - ( 7 - $day );
$int=new DateInterval( sprintf('P%sD', $subtract ) );
$obj=new DateTime( $setDate );
$previous=$obj->sub( $int );
}else{
$previous=$obj->sub( new DateInterval('P2D') );
}
# create the future date ( add 25 days )
$int=new DateInterval('P25D');
$obj=new DateTime( $setDate );
$future=$obj->add( $int );
if( array_search( $future->format( $format ), $hols ) ){
# Future date is a holiday... go back to previous working day
$day=$future->format('N');
$subtract = 2 - ( 7 - $day );
$int=new DateInterval( sprintf('P%sD',$subtract ) );
$future=$future->sub( $int );
}
}else{
# Given date is NOT a holiday...
# take a copy of the original DateTime object for generating future date.
$ref=new DateTime( $setDate );
$int=new DateInterval( 'P2D' );
$previous=$obj->sub( $int );
$day=$previous->format('N');
# Is this a holiday?
if( $day > 5 ){
# yes - go back to previous working day
$subtract = 2 - ( 7 - $day );
$int=new DateInterval( sprintf('P%sD',$subtract ) );
$previous=$previous->sub( $int );
}
$int=new DateInterval('P25D');
$future=$ref->add( $int );
$day=$future->format('N');
# Is this a holiday?
if( $day > 5 ){
$subtract = 2 - ( 7 - $day );
$int=new DateInterval( sprintf('P%sD',$subtract ) );
$future=$future->sub( $int );
}
}
printf(
'<pre>
Given date: %s
Previous (-2): %s
Future (+25): %s
</pre>',
$date,
$previous->format( $format ),
$future->format( $format )
);
Which yields:
Given date: 2022-05-01
Previous (-2): 2022-04-29
Future (+25): 2022-05-26
I want to calculate night hours (21PM to 6AM) between two given dates.
I don't have any ideas
public function biss_hours($start, $end){
$startDate = new \DateTime($start);
$endDate = new \DateTime($end);
$periodInterval = new \DateInterval( "PT1H" );
$period = new \DatePeriod( $startDate, $periodInterval, $endDate );
$count = 0;
foreach($period as $date){
$startofday = clone $date;
$startofday->setTime(5,59);
$endofday = clone $date;
$endofday->setTime(20,59);
if($date > $startofday && $date < $endofday){
$count++;
}
}
return $count;
}
I have this fonction but it's don't work :)
Thx any help
There is no need to loop over a period. As already suggested in the comments, start by calculating the hours of the start and end date since these are the ones that can actually contain less than a full nights hours. Once those are calculated, you can simply get the number of days remaining and multiply them by the amount of hours a night has.
Please note that I only consider hours in my example below, so 22:55 to 24:00 would result in 2 full hours of night time counted. Also it does not check cases where the end date is before the start date or whether inputs are valid. It should get the idea across though:
function getHoursForSingleDay ( $startTime, $endTime, $nightStart, $nightEnd ) {
$numHours = 0;
// if the day starts before night ends
if( $startTime < $nightEnd ) {
// e.g. Night ends at 6a.m. - day starts at 5 a.m. = 1 hour
$numHours += $nightEnd - $startTime;
}
// if the day ends after night starts
if( $endTime > $nightStart ) {
// e.g. day ends at 23 - night starts at 21 = 2 hours
$numHours += $endTime - $nightStart;
}
return $numHours;
}
function biss_hours ( $start, $end, $nightStart = 21, $nightEnd = 6 ) {
$startDate = new \DateTime( $start );
$endDate = new \DateTime( $end );
$startTime = intval( $startDate->format( 'H' ) );
$endTime = intval( $endDate->format( 'H' ) );
// Both dates being the same day is an edge case
if( $startDate->format( 'Y-m-d' ) === $endDate->format( 'Y-m-d' ) ) {
return getHoursForSingleDay( $startTime, $endTime, $nightStart, $nightEnd );
}
// get the hours for bot the start and end date, since they can be less than a full night
$numHours = getHoursForSingleDay( $startTime, 24, $nightStart, $nightEnd );
$numHours += getHoursForSingleDay( 0, $endTime, $nightStart, $nightEnd );
// all remaining days in between can be calculated in a rather simple way
$nightHoursPerDay = $nightEnd + ( 24 - $nightStart );
// -1 because diff returns 1 for two adjacent days, but we treat the first and last day specially
$numDaysBetween = intval( $endDate->diff( $startDate )->format( "%a" ) ) - 1;
$numHours += $numDaysBetween * $nightHoursPerDay;
return $numHours;
}
Currently I have the following string.
$timeago = human_time_diff( get_the_time('U'), current_time('timestamp') );
print $timeago;
This returns results that looks like this.
1 min, 1 hour, 1 week, 1 month, and 1 year.
I am trying to figure out how I can make this work using an if statement to detect if the post has been posted within the past 5 hours, and if it has make it echo "NEW" but if not, don't echo anything.
EDIT:
I tried the following with no success... I am getting confused on how to make it check for the hours portion as well as the number portion I guess.
$timeago = human_time_diff( get_the_time('U'), current_time('timestamp') );
print $timeago;
if( $timeago >= 0 && $timeago <= 5 )
{
print 'NEW';
}
You have to do it manually like below:
$from = current_time('timestamp') - 1000000;
$to = current_time('timestamp');
$diff = (int) abs($to - $from);
$hours = round($diff / HOUR_IN_SECONDS);
if ($hours <= 1)
$hours = 1;
echo $hours; //OP = 278 (278 hours)
Is there a way to only show the hours of a time if it's not 30 minutes past, but show :30 for when the time is 30 minutes past?
http://new.clairvoyant.co/details/?Pin=4378 This is my page. In the schedule you can see when the blocks are really short, it's a real struggle to display the time period within them.
What'd I'd like to do is to show a format like 12pm - 4pm if the start and end time are just hours. And show 12pm - 4:30pm if one of them is 30 minutes.
Is this able to done just by formatting strttotime? The input variable is in 24hour (24:00:00). The ending output is formatting into 12 hour.
$Start12 = strtotime($Shift['Start']);
$Stop12 = strtotime($Shift['Stop']);
And is outputted like:
<span class='c'>".date('g:ia',$Start12)." to ".date('g:ia',$Stop12)."</span>
Assuming that the only number of minutes will be 30:
function printDate30($date){
if(date('i',$date) == 30){
return date('g:ia',$date);
}else{
return date('ga',$date);
}
}
and use
echo "<span class='c'>".printDate30($Start12)." to ".printDate30($Stop12)."</span>";
Demo: http://codepad.org/c2AhcELC
Here's a good example mate, I've not syntax checked this though:
// Create a new object from your start time
$firstDateTime = new DateTime( strtotime( $Shift['Start'] ) );
$firstDateTimeFormatter = 'H'; // Set the object to be default formatted for the hour only
// Repeat, blah...
$secondDateTime = new DateTime( strtotime( $Shift['Stop'] ) );
$secondDateTimeFormatter = 'H';
// If the minutes for this object are not 00 then we want to format the time
// differently, these two checks do that
if( $firstDateTime->format('i') != '00' )
$firstDateTimeFormatter = 'H:m';
if( $secondDateTime->format('i') != '00' )
$secondDateTimeFormatter = 'H:m';
// See the results
echo $firstDateTime->format( $firstDateTimeFormatter );
echo $secondDateTime->format( $secondDateTimeFormatter );
// Put this into a convenient function to use
function formatTime( $time )
{
$dt = new DateTime( strtotime( $time ) );
return ( $dt->format('i') != '00' ? $dt->format('H:i') : $dt->format('H') );
}
I find it difficult to calculate time difference using PHP. How to calculate time difference in HH:MM:SS format (hours:minutes:seconds) between two different dates?
For example, the input is:
$start_time : 19:30
$end_time : 7:30,
$currenttime = 21:30
I have to find out current time has match with given start time and end time.
Convert the times to a timestamp value, which is an integer value in seconds. Then, you can subtract them easily to get the difference in seconds. After that, it's pretty straightforward to convert that back to hours and minutes.
$iStart = strtotime( '00:00:00' );
$iEnd = strtotime( '12:00:00' );
$iCurrent = strtotime( date('H:i:s') );
if( $iCurrent > $iStart && $iCurrent < $iEnd ) {
echo "active!";
} else {
echo "not active";
}
This will only respect the time (it will be active once a day).
If you need specific dates you can use:
$iStart = strtotime( '2012-12-01 00:00:00' );
$iEnd = strtotime( '2012-12-31 12:00:00' );
$iCurrent = time();
if( $iCurrent > $iStart && $iCurrent < $iEnd ) {
echo "active!";
} else {
echo "not active";
}