pre select dates based on dropdown selection in bootstrap datepicker? - php

Anyone know, how I can achieve below:
If someone select the option "For weekend only" then all the weekend dates will be selected from the current month to the next month's specified date.
if someone select the option "For weekday only" then all the weekday dates will be selected from the current month to the next month's specified date.
if someone select the option "For Friday only" then all the Friday dates will be selected from the current month to the next month's specified date.
Something like above:
Anyone have any idea where I can start this with??
$('#dt_1').datepicker({
rtl: KTUtil.isRTL(),
todayHighlight: true,
templates: arrows,
startDate: date, //disable all old dates
setDate: date, //tomorrow's date allowed
multidate: true,
format: 'dd/mm/yyyy'
});

I've found the solution to achieve this with the help of another post of stackoverflow:
get weekend using php
PHP code:
//weekend.....
$now = strtotime("now");
$end_date = strtotime("+3 weeks");
$weekend_array = array();
while (date("d-m-Y", $now) != date("d-m-Y", $end_date)) {
$day_index = date("w", $now);
if ($day_index == 0 || $day_index == 6) {
// Print or store the weekends here.
//echo "<br>".$day_index."- Date: ".date("d-m-Y", $now);
$weekend_array[] = date("d-m-Y", $now);
}
$now = strtotime(date("d-m-Y", $now) . "+1 day");
}
//friday only....
$now = strtotime("now");
$end_date = strtotime("+3 weeks");
$friday_array = array();
while (date("d-m-Y", $now) != date("d-m-Y", $end_date)) {
$day_index = date("w", $now);
if ($day_index == 5) {
// Print or store the weekends here.
//echo "<br>".$day_index."- Date: ".date("d-m-Y", $now);
$friday_array[] = date("d-m-Y", $now);
}
$now = strtotime(date("d-m-Y", $now) . "+1 day");
}
//weekdays......
$now = strtotime("now");
$end_date = strtotime("+3 weeks");
$weekday_array = array();
while (date("d-m-Y", $now) != date("d-m-Y", $end_date)) {
$day_index = date("w", $now);
if ($day_index >= 1 && $day_index <= 5) {
// Print or store the weekends here.
//echo "<br>".$day_index."- Date: ".date("d-m-Y", $now);
$weekday_array[] = date("d-m-Y", $now);
}
$now = strtotime(date("d-m-Y", $now) . "+1 day");
}
Javascript code:
$(document).ready( function () {
$("#sel_dropdown").change(function() {
$( "select option:selected" ).each(function() {
var v = $(this).val();
if(v == 1) {
//weekend only....
$('#dt_1').datepicker('setDates',[<?php
$i=0;
for($i=0;$i<=count($weekend_array);$i++) {
echo "'".$weekend_array[$i]."',";
}
?>]);
}
if(v == 2) {
//Friday only.....
$('#dt_1').datepicker('setDates',[<?php
$i=0;
for($i=0;$i<=count($friday_array);$i++) {
echo "'".$friday_array[$i]."',";
}
?>]);
}
if(v == 3) {
//Weekdays only....
$('#dt_1').datepicker('setDates',[<?php
$i=0;
for($i=0;$i<=count($weekday_array);$i++) {
echo "'".$weekday_array[$i]."',";
}
?>]);
}
if(v == 4) {
//custom option....
$('#dt_1').data('datepicker').setDate(null);
}
});
});
});
</script>
Hopefully, this will useful for anyone looking for this type of solution.

Related

How do I know in Magento if a given date is today / tomorrow / some day ago?

I would like to check, if a date is a today, tomorrow, yesterday, or Previous.
But my code doesn't work.
public function istimedate($timestamp=''){
$timestamp=date_create($timestamp);
$date = strtotime(date($timestamp,"Y-m-d"));
$current_Time = strtotime(date("Y-m-d"));
if($date == $current_Time) {
$day_time = 'Today';
} elseif(strtotime("-1 day", $date) == $current_Time) {
$day_time = 'Yesterday';
} elseif(strtotime("+1 day", $date) == $current_Time) {
$day_time = 'Tomorrow';
} else {
$day_time = 'Someday ago';
}
return $day_time;
}
$day = $this->istimedate('2021-01-01 09:33:04');
A date can be converted to the proper format using $date = strtotime(date_format($timestamp,"Y-m-d")); instead of $date = strtotime(date($timestamp,"Y-m-d"));.

How to check if the next day is available before the previous day in blade (Laravel)

I want to check when the store will be open:
if today is Wednesday and the store is close then it will say will
open on Thursday or Tomorrow.
if not Thursday will go to check Friday and so on but the issue is
it will first check Monday as the number of day is 1.
Then if available the message is the store will open on Monday which is not correct.
So I need to check first the days numbers: 4,5,6,7 then if not available check 1,2,3.
I tried with this but doesn't work.
foreach($storedays as $storeday)
{
if($storeday->storeinfo_id == $item->id)
{
$store_open = \Carbon\Carbon::createFromFormat('H:i', $storeday->open_time)->format('H:i');
$store_close = \Carbon\Carbon::createFromFormat('H:i', $storeday->close_time)->format('H:i');
if ($todayTime >= $store_open && $todayTime <= $store_close && ($storeday->available == 1) && ($storeday->day_id == $today))
{
$is_open = 1;
$when_open = 'now';
break;
}
else
{
if(($storeday->available == 1) AND ($storeday->day_id == $today))
{
$when_open_day = $storeday->day_id;
$open_day = date('l', strtotime("Sunday +$when_open_day days"));
$when_open_time = date('h:i a', strtotime($store_open));
if($storeday->day_id==$today)
{
$open_at = 'now';
}
else
{
$open_at=date('D', strtotime("Sunday +$when_open_day days"));
}
break;
}
else if(($storeday->available == 1) AND ($storeday->day_id < $today))
{
$when_open_day = $storeday->day_id;
$open_day = date('l', strtotime("Sunday +$when_open_day days"));
$when_open_time = date('h:i a', strtotime($store_open));
$open_at=date('D', strtotime("Sunday +$when_open_day days"));
break;
}
}
}
}

Dynamically Change Dates for an In Between Event

Based on a piece of coding from Rounin - I have taken this to try and come up with a method of, basically if todays date is between say 2017-03-01 and 2017-04-30, then display rego form. If outside of these dates, then display "Form Not available"
The date in between is two set dates is easy to understand, but what I want to do is dynamically change the Y so I don't have to edit manually every year.
What I have is (although seems very unwieldy - but works?)
<?php
$today = date('Y-m-d');
$Current_Year = date('y');
$Current_Month = date('n');
$Current_Day = date('j');
$Next_Year = ($Current_Year + 1);
if ($Current_Month < 5) {
$Begin_Rego_Year = $Current_Year;
}
if ($Current_Month > 3) {
$Begin_Rego_Year = $Next_Year;
}
# Checks if date is March 31st
if (($Current_Month == 3) && ($Current_Day == 1)) {
$Begin_Rego_Year = $Current_Year;
}
$Begin_Rego_Date = $Begin_Rego_Year.'-03-01';
/////////////////
if ($Current_Month < 5) {
$End_Rego_Year = $Current_Year;
}
if ($Current_Month > 3) {
$End_Rego_Year = $Next_Year;
}
# Checks if date is March 31st
if (($Current_Month == 4) && ($Current_Day == 30)) {
$End_Rego_Year = $Current_Year;
}
$End_Rego_Date = $End_Rego_Year.'-04-30';
if (($today >= $Begin_Rego_Date) && ($today <= $End_Rego_Date))
{
echo "Display Rego Form"; //if todays date was between 01 Mar - 30 Apr 17
}
else {
echo "Rego Form Offline until 01 Mar 18";
}
?>
Easist way to work with dates i think it is a function strtotime
#usage example strtotime();
if (
strtotime('today') >= strtotime('first day of this month')
&& strtotime('today') <= strtotime('last day of this month')
) {
echo 'display rego form';
}
php.net
Info about this function
Info about date formats for strtotime
Not sure if this is legal.. strtotime('last day of april') - couldn't make it work.
Ended up with (so far)
$open_day = date('01-03-Y');
$close_day = date('30-04-Y');
if (
strtotime('today') >= strtotime($open_day) && strtotime('today') <= strtotime($close_day)
) {
echo 'display rego form';
}
else {
echo 'Come back another time';
}

I want to update my date every wednesday at 12:00 A.M. in the dropdown in php

I want to update my date every wednesday at 12:00 A.M. in the dropdown in php and remains unchanged till next wednesday.
Following is my code:
$now = time(); // current timestamp
$today = date("w", $now); // "w" returns the weekday (number)
$wednesday = 3; // 5th day of the week (sunday = 0)
if ($today == $wednesday) {
$ts = $now; // today is wednesday, don't change the timestamp
$daysLeft = 0; // no days left!
} else {
$daysLeft = $wednesday-$today; // get the left days
$ts = $now + 84600 * $daysLeft; // now + seconds of one day * days left
}
?>
<h1>
Forecast for <?php echo date("Y-m-d", $ts) ?>
</h1>
In it date remains unchanged on wednesday which is correct and then change quickly as soon as thursday begins. Although I want it to remain same till next wednesday.
I think you are over-complicating the issue.
<?php
$today = time();
$nextWed = strtotime('next wednesday');
if(date('D', $today) === 'Wed') {
$ts = date('Y-m-d', $today);
} else {
$ts = date('Y-m-d', $nextWed);
}
echo '<h1>Forecast for '.$ts.'</h1>';
?>
What's happening?
Get timestamp for today
Get timestamp for next Wednesday
If today is Wednesday, $ts = today's date
If today is NOT Wednesday, $ts = next Wednesday's date
Echo your results
EDIT
<?php
$now = time();
$today = date('Y-m-d', $now);
if(date('D', $now) === 'Wed') { $nextWed = strtotime($today); }
if(date('D', $now) === 'Thu') { $nextWed = strtotime("$today - 1 days"); }
if(date('D', $now) === 'Fri') { $nextWed = strtotime("$today - 2 days"); }
if(date('D', $now) === 'Sat') { $nextWed = strtotime("$today - 3 days"); }
if(date('D', $now) === 'Sun') { $nextWed = strtotime("$today - 4 days"); }
if(date('D', $now) === 'Mon') { $nextWed = strtotime("$today - 5 days"); }
if(date('D', $now) === 'Tue') { $nextWed = strtotime("$today - 6 days"); }
$ts = date('Y-m-d', $nextWed);
echo '<h1>Forecast for '.$ts.'</h1>'
?>

Want Current Months All Date in PHP

i have a Date array like this
$dateArray = array('2012-04-11','2012-04-29','2012-04-26','2012-04-23','2012-03-21','2012-07-23','2012-12-19','2012-04-27','2012-05-12','2012-05-27','2012-05-28');
and i m using code to filter this array is
but it's not working
$start_date= date('Y-m-d',strtotime('first day of this month')) ;
$end_date =date('Y-m-d',strtotime('first day of next month')) ;
$month = array();
foreach ($dateArray as $dates)
{
$dateTimes = strtotime($dates);
if (($start_date >= $dateTimes) && ($end_date <= $dateTimes))
{
$month[]= $dates;
}
}
var_dump($month);
but it's not working
You need to compare unix timestamps to do the comparison properly:
$dateArray = array('2012-04-11','2012-04-29','2012-04-26','2012-04-23','2012-03-21','2012-07-23','2012-12-19','2012-04-27','2012-05-12','2012-05-27','2012-05-28');
$start_date = strtotime('first day of this month') ;
$end_date = strtotime('first day of next month') ;
$month = array();
foreach ($dateArray as $dates)
{
$dateTimes = strtotime($dates);
if (($start_date >= $dateTimes) && ($end_date <= $dateTimes))
{
$month[]= $dates;
}
}
var_dump($month);
Hi If you want to show only dates from array having current month then
foreach($dateArray AS $dateArr){
if(date('M') == date('M' , strtotime($dateArr))){
echo $mnthDate[] = $dateArr;echo "<br/>";
}
}
Else if also want to check both year and month same as current year and month the.
foreach($dateArray AS $dateArr){
if((date('M') == date('M' , strtotime($dateArr))) && (date('Y') == date('Y' , strtotime($dateArr)))){
$yrDate[] = $dateArr;echo "<br/>";
}
}

Categories