Carbon setTestNow function is not working - laravel-5.6

I am working on dates in Laravel. I have to set dates for patient future injections.
To keep it simple, let's suppose today is 13-03-2019 (Wednesday).
I created first date as:
$firstDate = Carbon::create(2019,03 ,18, 12); // The day is Monday
// set date
Carbon::setTestNow($firstDate);
Now I want the next two appointments should be on Wednesday and Friday. So I again set the dates as follow:
// set second date
$secondDate = new Carbon('Wednesday');
Carbon::setTestNow($secondDate);
// set thirdDate
$thirdDate = new Carbon('Friday');
Carbon::setTestNow($thirdDate);
According to above example the output should be:
2019-03-18
2019-03-20
2019-03-22
But the problem is that it outputs the first set date correct but print the 2nd and 3rd date wrong as it considers 'Wednesday' of next week as today's date.
So the Output print as:
2019-03-18
2019-03-13
2019-03-14
I have spent a lot of time on it, I would appreciate if anyone of you people could help me in this.
I would appreciate if anyone guides me where I am going wrong.
Thanks.

As the setTestNow() function was not working for 2nd and third date/days, so I first get all three required days then convert them to 'dayOfWeek' which returns day number (Sunday 0, Monday 1 and so on...). I subtracted the first day from second and third day and then finally add these days to the date that i get from the datepicker.
// set the start date
if( $visitstart_date != null && $visitstart_date != '') {
Carbon::setTestNow($visitstart_date);
} else {
Carbon::setTestNow();
}
if($perweek_visit1_day != '')
{
//Get first selected day number
$firstDay = Carbon::parse($perweek_visit1_day)->dayOfWeek;
$perweek_visit1_dayDate = Carbon::now();
}
if($perweek_visit2_day != '')
{
//Get second day numer
$secondDay = Carbon::parse($perweek_visit2_day)->dayOfWeek - $firstDay;
$perweek_visit2_dayDate = Carbon::now()->addDays($secondDay);
}
if($perweek_visit3_day != '')
{
//Get third day number
$thirdDay = Carbon::parse($perweek_visit3_day)->dayOfWeek - $firstDay;
$perweek_visit3_dayDate = Carbon::now()->addDays($thirdDay);
}

Related

How to check if month change?

I want to update all my depriciation cost each month for all items until 0
Example.
Depriciation has value of 111 . each month it should be depreciated until depriciation cost
reach 0 or less than
controller
all_item=assets::all();
foreach($all_item as $item)
{
//should i make static month to compare?
$month=Carbon::now()->format('m');
$update_item = assets::findOrFail($item->id);
$update_item->depriciation_cost = $item->depriciation - $item->depriciation_cost;
$update_item->update();
}
You can create a new column in the table to keep track of the last depreciation value updated like depreciation_updated_at (type: date) OR use created_at/updated_at column to get the last update month (if no other reason to update record).
Then simply use updated version of your code:
all_item=assets::all();
foreach($all_item as $item)
{
$month=Carbon::now()->format('m');
$depreciation_updated_at = createFromFormat('Y-m-d', $item->depreciation_updated_at); //or can use created_at/updated_at as said below
if ($depreciation_updated_at ->format('m') != $month) {
$update_item = assets::findOrFail($item->id);
$update_item->depriciation_cost = $item->depriciation - $item->depriciation_cost;
$update_item->update();
}
}
Hope it will do your task.

Vue js & validate DOB is within a range?

Using Vue js and v-validate how can I determine if the date of birth is greater than 21 and less then 55 years old? Any help is greatly appreciated.
import * as moment from "moment";
let birthday = moment(moment.now()).diff(moment(this.user.day + this.user.month + this.user.year, "DD.MM.YYYY"),"years");
if(birthday > 21 && birthday < 55) {
// do next steps
}
Moment(npm install moment) is used to parse, manipulate & display dates and times in JavaScript. moment.now() will give the present date and assuming you have three fields for day, month and year in different variables, format it and use the diff function to find the age.
The example from Madhuri works for me but i change the input as hard coded string.
enter code here let birthday = moment(moment.now()).diff(moment('01.01.1990', "DD.MM.YYYY"), "years");
if(birthday >= 20 && birthday <=70 ){
return birthday;
}
}
I solved it with with value from form input which works for me.
checkBirthday(){
const birthDayDate = document.getElementById("birthdate").value;
const age = moment().diff(birthDayDate, "years");
// let birthday = moment(moment.now()).diff(moment('01.01.1990', "DD.MM.YYYY"), "years");
if(age >= 18 && age <=74 ){
return age;
}
},

KendoUI scheduler Week view :-how to get date of start of week

I am using KendoUI scheduler in my application. is there any way to get the start of week when the selected view is week.
Yes. Use the navigate event to catch current date and get the start of week by manipulating the retrieved date. i.e.
navigate: function (e) {
if (e.view.toLowerCase() === "week") {
GetStartDateOfWeek(e.date);
}
}
function GetStartDateOfWeek(d) {
d = new Date(d);
var day = d.getDay(),
diff = d.getDate() - day + (day == 0 ? -6:1); // adjust when day is sunday
var startOfWeek = new Date(d.setDate(diff));
}
Hope it helps!
Reference: JavaScript - get the first day of the week from current date

How to Select a Week using kendodatepicker

I an using kendodate picker in my solution.I want to retrieve the dates for full week whenever user selects some date from the kendodatepicker.e.g.lets say today is Feb 21,2014(Friday) whenever user selects some day say Feb 24,2014(Monday) then i need to retrieve its week's dates(starting and ending date).the answer should be(Feb 22,2014-Feb 28,2014)considering the fact that my week starts from Saturday(Feb 22)and ends on Friday(Feb 28).Kindly guide.i am new very much new to kendoUI.thanks
Define the change event handler as:
change : function (e) {
// Get a reference to the row (week) containing the selected date
var tr = $(".k-state-selected", cal._table).closest("tr");
// Get the first day
var first = $("td:first", tr).text();
// Get the last day
var last = $("td:last", tr).text());
}
Example here : http://jsfiddle.net/OnaBai/W9VFB/9/
If you want to have the first and last date as text, then you might use:
var first = $("td:first", tr).find("a").attr("title");
var last = $("td:last", tr).find("a").attr("title");
Example here : http://jsfiddle.net/OnaBai/W9VFB/12/

How to show reminder by month for Windows phone

How to show reminder by month?
I have created this partial code and I am stucked how to manipulate to check and get reminder by month.
reminders = ScheduledActionService.GetActions<Reminder>();
if (reminders.Count<Reminder>() > 0)
{
}
else
{
}
If there is reminder I want to check the month and get the month i want.
try this one:
var month = 5; // your month
var remindersOfMonth = ScheduledActionService.GetActions<ScheduledAction>()
.Where(a=> a.BeginTime.Month == month);
According to this when you use GetActions() it will return you a list of
ScheduledAction. Now according to this ScheduledAction has a property called BeginTime which is of type DateTime so it has Month property which is your interest.
variable "a" in the statement is in fact each of ScheduledAction in the collection which are begin evaluated again the month.

Resources