Timer Countdown (Confused) - php

Basically, I am trying to make a timer or countdown in PHP. This will take the time saved in the threads (date of post) and then show how long ago that is in comparison to the current time.
Anybody know how to do this?
Code:
<?php foreach($this->updated as $thread){ ?>
<section>
<div class="body">
<?php if($this->threads[$thread]['image']!='') { ?>
<div>
<a href="<?php echo $this->threads[$thread]['image']; ?>">
<img src="<?php echo $this->threads[$thread]['image']; ?>">
</a>
</div>
<?php } ?>
<?php echo $this->threads[$thread]['post']; ?>
</div>
<div class="small text-right">
<?php echo count($this->threads[$thread]['posts']); ?> Replies
|
<?php echo date("Y-m-d H:i:s", $this->threads[$thread]['time']); ?> EST
|
View
</div>
</section>
<?php } ?>
I don't want it to be an exact timer, more like it would say for example "less than a minute ago", "1 minute ago", "2 minutes ago", etc...
I have tried to make 2 different variables and then did $current->diff($date); Then just echo $diff->h for example for the hour. But I was confused by the matter.

You can use DateTime::diff function:
<?php
date_default_timezone_set('Europe/Moscow');
$timestamp = 1421253112;
$date = new DateTime;
$date->setTimestamp($timestamp);
$now = new DateTime;
$interval = $now->diff($date);
echo $interval->format('%i minutes ago');
Output:
7 minutes ago

Related

Conditional statement not giving desired output in PHP 7.4

I have a PHP script that queries the database and produces results based on the condition. In PHP 7.1 all worked well. But the server the website is hosted has requested I change the server PHP language to 7.4.
After the change I noticed the issue of the desired results not appearing as it should.
Below are my codes.
<?Php if (($checkday != "Sunday")&&($phdate == "")) {
$davsquery = "SELECT davsID, davs, davsslots FROM dailyavailableslotsus WHERE YEAR(davs)=? AND MONTH(davs)=? AND DAY(davs)=?";
$davsstmt = $connQlife->prepare($davsquery);
$davsstmt->bind_param('sss', $year, $month, $day);
$davsstmt->execute();
$davsstmt->store_result();
$davsstmt->bind_result($davsID, $davs, $davsslots);
$davsstmt->fetch();
$davsstmt->close();
if (($davsslots!="")&&($totalnumrows!="")) {
$availableslots = $davsslots - $totalnumrows;
}
if (($davsslots!="")&&($totalnumrows=="")) {
$availableslots = $davsslots;
}
if (($totalnumrows=="")&&($davsslots=="")) {
$availableslots = $defaultusimslots;
}
if (($checkday == "Saturday")&&($davsslots=="")) {
$availableslots = $usimslots20 - $totalnumrows;
}
if (($checkday == "Saturday")&&($davsslots!="")) {
$availableslots = $davsslots - $totalnumrows;
}
?>
<div class="calendarheadercont">
<div class="calendarday"><?Php echo date("l", mktime(0, 0, 0,$month,$day,$year)); ?></div>
</div>
<div class="clear_1"></div>
<div class="calendarsubcont">
<div class="calendardatecont">
<div class="calendarmonth"><?Php echo date("M", mktime(0,0,0, $month,$day,$year)); ?></div>
<div class="calendardate"><?Php echo date("j", mktime(0,0,0,$month,$day,$year)); ?></div>
</div>
<?Php if ($day > $curday){ ?>
<div class="calendartextcont">
<?Php if ($availableslots > 5){ ?>
<div class="calendartextg">Available slots: <?Php echo $availableslots; ?></div>
<?php } ?>
<?Php if (($availableslots >= 1)&&($availableslots <= 5)) { ?>
<div class="calendartexto">Available slots: <?Php echo $availableslots; ?></div>
<?php } ?>
<?Php if ($availableslots <= 0){ ?>
<div class="calendartextr">No Slots Available!</div>
<?php } ?>
</div>
<?Php if ($availableslots >= 1){ ?>
<label class="calendarcheckbox">Select
<input type="checkbox" name="screening" id="screening" value="<?Php if ($screeningdate=="") {echo date('Y-m-d', mktime(0,0,0,$month,$day,$year)); }else{ echo $screeningdate; } ?>">
<span class="checkmark"></span>
</label>
<?php } ?>
<?php } else { ?>
<div class="calendartextcont">
<div class="calendartextr">Booking Closed!</div>
</div>
<?php } ?>
</div>
<?Php } ?>
<?Php if ($checkday == "Sunday") { ?>
<div class="calendarheadercont">
<div class="calendarday"><?Php echo date("l", mktime(0, 0, 0,$month,$day,$year)); ?></div>
</div>
<div class="clear_1"></div>
<div class="calendarsubcont">
<div class="calendardatecont">
<div class="calendarmonth"><?Php echo date("M", mktime(0,0,0, $month,$day,$year)); ?></div>
<div class="calendardate"><?Php echo date("j", mktime(0,0,0,$month,$day,$year)); ?></div>
</div>
<div class="calendartextcont">
<div class="calendartextr">Closed On Sundays!</div>
</div>
</div>
<?Php } ?>
<?Php if ($phdate != ""){ ?>
<div class="calendarheadercont">
<div class="calendarday"><?Php echo date("l", mktime(0, 0, 0,$month,$day,$year)); ?></div>
</div>
<div class="clear_1"></div>
<div class="calendarsubcont">
<div class="calendardatecont">
<div class="calendarmonth"><?Php echo date("M", mktime(0,0,0, $month,$day,$year)); ?></div>
<div class="calendardate"><?Php echo date("j", mktime(0,0,0,$month,$day,$year)); ?></div>
</div>
<div class="calendartextcont">
<div class="calendartextr"><?Php echo $phdetail; ?></div>
</div>
</div>
<?Php } ?>
What changed in PHP 7.4 that may be affecting the script? Or is there a better way to rewrite this that produces the same results but in code practice favorable to PHP 7.4
To put this into context, this is a subset of a larger script that loops through the days of each month and shows how many slots are available for someone who wants to make a booking for that day.
There is a default value of 30 slots per day. There is also a table that takes entries of a particular number of slots for any particular day specified. There is also a table that takes public holidays. The script is supposed to check if there is a manual entry of slots for a particular day and if there is, check the number of slots available for that day and subtract from the manual entry to determine how many slots are left. If not manual entry, the default slots is then used for that day.
On PHP 7.4, if I specify a manual entry of say 10 slots for a particular day, the output when the entire script is run is that all days of the current month now shows 10 as the slots available rather than 30 and only show 10 for the particular day specified.
The other issue I noticed is that, if I specify a public holiday, all days after that day also show as the public holiday.
But when I switch back to PHP 7.1 all works perfectly.

I want to compare two date with current date in if condition

In my data base I have start_date and end_date for my course pack and I want to show my div in between course of start date and date end date.
In my current course start date and end date have,
start_date = 2019-08-01
end_date = 2019-09-01
My code:
<?php
date_default_timezone_set('Asia/Kolkata');
$date = date('Y-m-d', time());
foreach ($fetchyotubedetail as $fetchyoutubedetails) :
$currentData = date_create($fetchyoutubedetails['start_date']);
$expiryDate1 = date_create($fetchyoutubedetails['end_date']);
if ($date <= $currentData && $date <=$expiryDate1){
echo 'hi';
}else{
?>
<?php
?>
<div class="col-lg-4 col-md-4 col-sm-6 " style="position: relative;">
<iframe id="<?php echo $fetchyoutubedetails['id'];?>" class="yt_players" width="972" height="284" src="<?php echo $fetchyoutubedetails['video_links']?>?rel=0&wmode=Opaque&enablejsapi=1;showinfo=0;" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture">
</style> allowfullscreen></iframe>
<h3 class="category-name">Category:<?php echo $fetchyoutubedetails['course_title'];?></h3><!-- /.category-name -->
<h2 class="entry-title"><?php echo $fetchyoutubedetails['video_title'];?></h2>
<span><i class="fa fa-clock-o"></i> <time datetime="PT5M">Cource Duration : <?php echo $fetchyoutubedetails['cource_duration'] ?> Month</time></span>
</div>
<?php
}
?>
<?php endforeach; ?>
I do not know where I am wrong in my if ($date <= $currentData && $date <=$expiryDate1) condition.
If my course pack date is in between start and end date then only show my course div other wise show only echo 'hi';
The main problem is, you compare apples and oranges here...
date returns a string, formatted according to the given format
date_create on the other hand is an alias for DateTime::construct() which returns a DateTime Object.
So in your specific case the following should work:
$objDateNow = new DateTime();
$objDateNow->setTimezone(new DateTimeZone('Asia/Kolkata'));
foreach($fetchyotubedetail as $fetchyoutubedetails)
{
$objDateStart = DateTime::createFromFormat('Y-m-d', $fetchyoutubedetails['start_date']);
$objDateStart->setTimezone(new DateTimeZone('Asia/Kolkata'));
$objDateEnd = DateTime::createFromFormat('Y-m-d', $fetchyoutubedetails['end_date']);
$objDateEnd->setTimezone(new DateTimeZone('Asia/Kolkata'));
if ($objDateNow >= $objDateStart && $objDateNow <= $objDateEnd)
{
echo 'hi';
}
else
{
//...
}
}
For more information take a look at the PHP official docs
https://www.php.net/manual/en/function.date.php
https://www.php.net/manual/de/function.date-create.php

jquery datepicker - compare months of two dates

I am trying to compare the months of two dates using the jquery datepicker. I want it to check and see if my end date has the same month as my start date so that I can add in the end date month if needed:
Instead of displaying:
Event Dates
January 1st - 1st, 2017
March 3rd - 4th, 2017
I want to display:
Event Dates
January 1st - February 1st, 2017
March 3rd - 4th, 2017
This is the code I have so far. PS. I am using advanced custom fields within wordpress. That is why get_field('event_start_date') pulls in the date. The only argument I really need help with is the <?php if ($endDate[M] != $startDate[M]): ?> code. That was just my best guess on how to do this but it does not work at all.
<p>
<?php if( get_field('event_start_date') ): ?>
<?php $startDate = DateTime::createFromFormat('Ymd', get_field('event_start_date')); ?>
<?php echo $startDate->format('F'); ?> <?php echo $startDate->format('jS'); ?>
<?php if( get_field('event_end_date') ): ?>
<?php $endDate = DateTime::createFromFormat('Ymd', get_field('event_end_date')); ?>
-
<?php if ($endDate[M] != $startDate[M]): ?>
<?php echo $endDate->format('F'); ?>
<?php endif; ?>
<?php echo $endDate->format('jS'); ?>, <?php echo $endDate->format('Y'); ?>
<?php else : ?>
, <?php echo $startDate->format('Y'); ?></span>
<?php endif; ?>
<?php endif; ?>
</p>

How to show in php only current weekday.After passing of current week only then next weekdays show

I have this code.That is showing 5 business days(that is correct). but i want only current weekdays even that is passed. after passing of current week next weekdays shows
<?php for($i=0;$i<=4;$i++): ?>
<label ><?php echo date('d-M-Y', strtotime("+$i Weekday")); ?></label>
<label ><?php echo date('l',strtotime("+$i Weekday")); ?></label><br>
<?php endfor; ?>
Its output is:
19-Nov-2014 Wednesday
20-Nov-2014 Thursday
21-Nov-2014 Friday
24-Nov-2014 Monday
25-Nov-2014 Tuesday
This is also showing next week days.
what i want is like this:
17-Nov-2014 Monday
18-Nov-2014 Tuesday
19-Nov-2014 Wednesday
20-Nov-2014 Thursday
21-Nov-2014 Friday
Try this:
$monday_this_week = date("Y-m-d",strtotime('monday this week'));
<?php for($i=0;$i<=4;$i++): ?>
<?php $date = date('d-M-Y', strtotime("+$i days", strtotime($monday_this_week))); ?>
<label ><?php echo $date; ?></label>
<label ><?php echo date('l', strtotime($date)); ?></label><br>
<?php endfor; ?>

Coding Wordpress/PHP Problem

Im having a bit of a problem with wordpress. On my site there is a bit that displays voucher now what i want is when that voucher expires i want the voucher details div not too show.
Here is the code from single.php
<?php if(have_posts()) : while (have_posts()) : the_post();
$image = get_post_meta($post->ID, 'image',true);
$writer = get_post_meta($post->ID, 'writer',true);
$code = get_post_meta($post->ID,'vcode',true);
$store = get_post_meta($post->ID,'store',true);
$expiry = strtotime(get_post_meta($post->ID,'expiry',true));
$desc = get_post_meta($post->ID,'description',true);
$datetoday = strtotime(date('Y/m/d'));
?>
this brings all the data from custom fields
heres the voucher div
<?php if($expiry > $datetoday){?>
<div class="voucherDetails">
<h2>Voucher Details</h2>
<div class="left">
<ul>
<li>Code: <?php echo $code;?></li>
<li>Expires: <?php echo $expiry;?></li>
<li><a class="more-link" href="<?php echo $store;?>" title="Visit Store">Visit Store!</a></li>
</ul>
</div>
<div class="desc right">
<h4>Description</h4>
<p><?php echo $desc;?></p>
</div>
</div>
<?php }?>
Now when i run this with the expiry with any value the voucher doesn't show The value in store is DD/MM/YYYY
Can't work it out, all help appreciated,
Thanks
Joe
Change
$datetoday = strtotime(date('d/m/Y'));
to be
$datetoday = strtotime(date('Y/m/d'));
Try run the following as an example of how it differs:
$datetoday = strtotime(date('d/m/Y'));
var_dump($datetoday);
var_dump(date("F j, Y, g:i a", $datetoday));
$datetoday = strtotime(date('Y/m/d'));
var_dump($datetoday);
var_dump(date("F j, Y, g:i a", $datetoday));
Edit: Alternatively use strtotime("now") if you want the precise timestamp to the very minute

Categories