Using Carbon in Laravel to extract and format a date and time from 20220916#120000 - laravel

I'm getting a "date" value of "20220916#120000" and I have no idea how to use carbon to better format these to separate date and times.
I'm still learning, but this is what I have so far:
$weather_date = Carbon::createFromFormat('Ymd#His', $weather->features[0]->properties->date)->format('jS F Y h:i:s');
but the response I see when dd($weather_data) is 'Unexpected data found)
Could anyone please offer guidance/advise on what I need to do in order to have the date and time available in a way in which I can query against.
Thank you.

There's more elegant solutions to this question, but the simplest solution is to replace the '#' with a '-'. It should be the fastest solution to the issue, but will require a comment for explanation
$dateStr = "20220916#120000";
$dateStr = \Str::replace("#", "-", $dateStr);
$weather_date = \Carbon\Carbon::createFromFormat('Ymd-His', $dateStr)->format('jS F Y h:i:s');
This is the more elegant solution to the problem; using regular expressions. It's easier to read, but the end result is the same.
$dateStr = "20220916#120000";
$dateTimeStr = preg_replace("/^(\d{4})(\d{2})(\d{2})#(\d{2})(\d{2})(\d{2})$/", '\1-\2-\3 \4:\5:06', $dateStr);
$weather_date = \Carbon\Carbon::parse($dateTimeStr)->format('jS F Y h:i:s');

Related

Too many arguments have been given to this function

I am getting the error, stated in the title, with the following code:
ToText (Split ({?CutOffDate},"-") [2]),ToText (Split ({?CutOffDate},"-") [1]))
Please help fresh programmer find the problem!
Check first if this is a conversion issue, using the CDate function, as in here:
ToText(CDate({aString}), "dd-MMM-yy")
Don't forget the := assignment operator
EffectiveDateTimeString := ToText(CurrentDate + CurrentTime, "dd-MM-yyyy hh:mm:ss");

GAMS decimal numbers

I have the following code:
loop (d,
rnd(d)=uniformInt(1,nd)
);
I am going to use the integer numbers rnd(d) as an index of another set s(i). But for example when rnd(d)=34.000 however, it is integer, but s(34.000) has no valid index since, 34.000 is not 34 !! and GAMS shows a error message.
I don't know if #Lutz solution worked out for you. In case it did not, you can try the following:
First, it is not necessary to loop over the set d, just a simple:
rnd(d) = uniformInt(1,nd);
will suffice.
Next line can go like:
loop(d,
s(i)$(i.val = ord(d)) = . . .;
);
If you still have issues, then using #Lutz suggestion just append '*1.000' to 'ord(i)' and/or 'rnd(d)', whichever is giving you issues.
Is i an ordered set? If yes, you could use something like this:
loop(d,
s(i)$(ord(i)=rnd(d)) = ...;
)

asp classic FormatNumber bidding script

Afternoon,
Im playing around with a little bidding script im trying to write. But im having trouble with formatNumber function.
currentBid = 50.51 'from database dataType double(16,2)
yourBid = isNumeric(Request("bid"))
If FormatNumber(yourBid,2) > FormatNumber(currentBid,2) Then
Response.Write"bid successful... woop woop"
else
Response.Write"you cant bid below the current asking price"
end if
But if i was to bid 1000 is writes "you cant bid below the current asking price"
Please advise
Regards
Shane
'Changed as advised
currentBid = 50.51 'value from database
If IsNumeric(Request.Form("bid")) Then
yourBid = CDbl(Request.Form("bid"))
end if
You have two issues here:
As Ekkehard mentioned, IsNumeric() returns a boolean. To test if the value is numeric and then store to your variable, use:
If IsNumeric(Request("bid")) Then yourBid = CDbl(Request("bid"))
FormatNumber() returns a string representation of a number. So you're comparing one string against another instead of one number against another. If you need to round your numbers to two decimals, use the Round() function instead:
If Round(yourBid,2) > Round(currentBid,2) Then
Edit: Proof.
MsgBox VarType(4) ' 2 = vbInteger
MsgBox VarType(FormatNumber(4)) ' 8 = vbString
The line
yourBid = isNumeric(Request("bid"))
does not store a valid number into yourBid, but the (booelan) result of the IsNumeric() function applied to Request("bid").
Change the line to
yourBid = CDbl(Request("bid"))
and see if your IF statement works as expected. Then add a proper validation for Request("bid").

LotusScript: how to iterate numbered fields with for loop

I'm using LotusScript to clean and export values from a form to a csv file. In the form there are multiple date fields with names like enddate_1, enddate_2, enddate_3, etc.
These date fields are Data Type: Text when empty, but Data Type: Time/Date when filled.
To get the values as string in the csv without errors, I did the following (working):
If Isdate(doc.enddate_1) Then
enddate_1 = Format(doc.enddate_1,"dd-mm-yyyy")
Else
enddate_1 = doc.enddate_1(0)
End If
But to do such a code block for each date field didnt feel right.
Tried the following, but that isnt working.
For i% = 1 To 9
If Isdate(doc.enddate_i%) Then
enddate_i% = Format(doc.enddate_i%,"dd-mm-yyyy")
Else
enddate_i% = doc.enddate_i%(0)
End If
Next
Any suggestions how to iterate numbered fields with a for loop or otherwise?
To iterate numbered fields with a for loop or otherwise?
valueArray = notesDocument.GetItemValue( itemName$ )
however do you know that there is a possibility to export documents in CSV format using Notes Menu?
File\Exort
Also there is a formula:
#Command([FileExport]; "Comma Separated Value"; "c:\document.csv")
Combined solution of Dmytro, clarification of Richard Schwartz with my block of code to a working solution. Tried it as an edit on solution of Dmytro, but was rejected.
My problem was not only to iterate the numbered fields, but also store the values in an iterative way to easily retrieve them later. This I found out today trying to implement the solution of Dmytro combined with the clarification of Richard Schwartz. Used a List to solve it completely.
The working solution for me now is:
Dim enddate$ List
For i% = 1 To 9
itemName$ = "enddate_" + CStr(i%)
If Isdate(doc.GetItemValue(itemName$)) Then
enddate$(i%) = Format(doc.GetItemValue(itemName$),"dd-mm-yyyy")
Else
enddate$(i%) = doc.GetItemValue(itemName$)(0)
End If
Next

Return popupmenu selection in MATLAB using one line of code

I have a GUI which uses a selection from a popupmenu in another callback. Is there a way to return the selected value of the popupmenu in only one line without creating any temporary variables? I've tried several solutions, but I've only managed two lines with one temporary variable:
Three lines:
list=get(handles.popupmenu1,'String');
val=get(handles.popupmenu1,'Value');
str=list{val};
Two lines:
temp=get(handles.popupmenu1,{'String','Value'});
str=temp{1}{temp{2}};
Can anyone shave it down to one?
PS, It's a dynamic menu, so I can't just use get(handles.popupmenu1,'Value') and ignore the string component altogether.
Here's a one-liner:
str = getCurrentPopupString(handles.popupmenu1);
And here's the definition of getCurrentPopupString
function str = getCurrentPopupString(hh)
%# getCurrentPopupString returns the currently selected string in the popupmenu with handle hh
%# could test input here
if ~ishandle(hh) || strcmp(get(hh,'Type'),'popupmenu')
error('getCurrentPopupString needs a handle to a popupmenu as input')
end
%# get the string - do it the readable way
list = get(hh,'String');
val = get(hh,'Value');
if iscell(list)
str = list{val};
else
str = list(val,:);
end
I know that's not the answer you were looking for, but it does answer the question you asked :)
I know this is stupid, but I couldn't resist:
list=get(handles.popupmenu1,'String'); str=list{get(handles.popupmenu1,'Value')};
I know that's not what you meant, but like the other answers above, it does answer your question... :-)
To make it a one-liner, I would simply create my own function (i.e. getMenuSelection) like Jonas illustrates in his answer. If you really want a true one-liner, here's one using CELLFUN:
str = cellfun(#(a,b) a{b},{get(handles.popupmenu1,'String')},{get(handles.popupmenu1,'Value')});
Very ugly and hard to read. I'd definitely go with writing my own function.
EDIT: And here's a slightly shorter (yet still equally ugly) one-liner using FEVAL:
str = feval(#(x) x{1}{x{2}},get(handles.popupmenu1,{'String','Value'}));

Resources