Laravel Carbon Date add 7 minutes [closed] - laravel

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
My request returns a date string
"07/13/2020 8:00 AM - 07/17/2020 5:00 PM"
Then I explode it
$datetimes = explode(' - ', $request->datetimes);
Then I get when I DD
"07/13/2020 8:00 AM"
When I use carbon to format the string
\Carbon\Carbon::parse($datetimes[0])->format('d M, H:mA')
It returns
"13 Jul, 08:07AM"
Why is this happening?

Because it's converting the date from the american format MM/DD/YYYY instead of the common DD/MM/YYYY
That why when handeling date formats, it's safer to use the mysql date format YYYY/MM/DD
it's how strtotime in php works, if the day is a valid month, it will use the MM/DD/YYYY format.
one easy trick is to replace the / with - to force it.
\Carbon\Carbon::parse(str_replace('/', '-', $datetimes[0]))

I was using "m" for month instead of "i" for minutes...

Can you please give this a try
Route::get('convert',function(){
$myString = "07/13/2020 8:00 AM - 07/17/2020 5:00 PM";
$dateTimes = explode(' - ', $myString);
$paseDateTime = \Carbon\Carbon::createFromFormat('m/d/Y g:iA',$dateTimes[0])->addMinutes(7)->format('m/d/Y g:iA');
return $paseDateTime;
});
Output
07/13/2020 8:07AM

Related

Laravel How to filter inputs before going to the DataBase? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
Laravel How to filter inputs before going to the DataBase
what shoud i do to filter my inputs before saving to the database? i want it to be uniformed in like this format "Name" a uper case followed by lowercase.
in some cases like when the user register a full caps name i want it to be re format as the example "Name"
there's an especific PHP function to do what you need: ucfirst(). This function turns to Uppercase the first char from a string.
For example:
//If $request->name is 'JoHn' or whatever
$filtered_name = ucfirst($request->name);
//Returns 'John'
If is a case with two names, you can use the ucwords() PHP function (Turn first letter uppercase of every word from a string)
//If $request->name is 'OlivER JAMES'
$filtered_name = ucwords($request->name);
//Returns 'Oliver James'
Hope this help you.

Unable to convert date with UTC using Joda Time API [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 1 year ago.
Improve this question
I'm trying to convert this date into local date
Input: "2021-04-20T15:00:00+02:00";
Expected output: "2021-04-20T13:00:00Z";
Actual output : "2021-04-20T15:00:00
Can you please let me know which library to use?
Code:
String date = "2021-04-20T15:00:00+02:00";
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ");
LocalDateTime dt = LocalDateTime.parse(date, formatter);
System.out.println(dt.toString());
You can use java-8 OffsetDateTime to parse the input string (since it is in ISO-8601) and then use toInstant
String timeStamp = "2021-04-20T15:00:00+02:00";
OffsetDateTime.parse(timeStamp).toInstant() //2021-04-20T13:00:00Z
tl;dr
You used the wrong type, LocalDateTime. Use OffsetDateTime and Instant.
OffsetDateTime
.parse( "2021-04-20T15:00:00+02:00" )
.toInstant()
.toString()
Details
The Joda-Time library is now In maintenance mode after having been succeeded years ago by the java.time classes defined in JSR 310 and built into Java 8 and later.
Your input has an indicator of an offset-from-UTC of +02:00, two hours ahead of UTC. So parse as a java.time.OffsetDateTime object.
OffsetDateTime odt = OffsetDateTime.parse( "2021-04-20T15:00:00+02:00" ) ;
Adjust to UTC by merely extracting an Instant.
Instant instant = odt.toInstant() ;
Generate your desired output.
String output = instant.toString() ;

Is there a human language that has months ordered alphabetically? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
So I like datestrings in ISO format ie 2021-12-25 - it means you can just apply a simple sort and dates are in the right order.
But sometimes it is nice to see months as English rather trying to remember if October is 8th or ninth or tenth month. (maybe its just me).
So 2021-Dec-25 is nice to read but I lose that sort quality - this date would now appear before Thanksgiving on 2021-Nov-25.
So I was wondering if there was a human language that can do both - is there a language where January comes before February in that language's alphabet. For example French fails here (Janvier > Fevrier).
Well, let's query the cultures: for each culture we get months' names and check if they are sorted (we compare original names with the sorted names).
C# code:
var result = CultureInfo
.GetCultures(CultureTypes.AllCultures)
.Where(ci => ci.DateTimeFormat
.MonthNames
.Where(month => !string.IsNullOrEmpty(month))
.Where(month => !Regex.IsMatch(month, "^M[0-9]+$"))
.Any())
.Where(ci => ci.DateTimeFormat
.MonthNames
.Where(month => !string.IsNullOrEmpty(month))
.SequenceEqual(ci
.DateTimeFormat
.MonthNames
.Where(month => !string.IsNullOrEmpty(month))
.OrderBy(month => month)))
.OrderBy(ci => ci.Name)
.Select(ci => $"{ci.Name} ({ci.EnglishName})");
Console.Write(string.Join(Environment.NewLine, result));
Outcome:
Empty
So, there are no such (lucky?) cultures on the Earth (at least known to .Net 5)
You can try AbbreviatedMonthNames, MonthGenitiveNames, but alas: the output will be empty as well.

GO: Date and Time Parsing in GO [duplicate]

This question already has answers here:
Parsing date/time strings which are not 'standard' formats
(4 answers)
Closed 7 years ago.
I have format string "day/month/year, hour:minute:second"
How do I parse into a time object?
I tried:
const longForm = "5/01/2015, 12:00:00"
t1, _ := time.Parse(longForm, "5/01/2015, 12:00:00")
0001-01-01 00:00:00 +0000 UTC
I get some UTC time, but this is not helpful if I want to compare times because I get the same UTC time for them all. Any help?
Rule #1 of fightclub, err Go, check your errors.
That being said, the format for time parsing is defined in the documentation (scroll down to constants).
For your specific question, the format is 1/_2/2006, 15:04:05.
playground

My date format : 09/28/2012 16:35:34 , I want the date format like : 2012-09-28T16:35:34 , using gsub [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Ruby - Change string in a date format to another format
My date format : 09/28/2012 16:35:34, I want the date to be formatted like: 2012-09-28T16:35:34, I need to compile the code in jruby.
You probably want to convert the date to something more useful:
require 'date'
dt = DateTime.strptime "09/28/2012 16:35:34", '%m/%d/%Y %H:%M:%S'
# => #<DateTime: 2012-09-28T16:35:34+00:00 (106107805067/43200,0/1,2299161)>
Now you can do any transformation:
dt.strftime '%FT%T'
# => "2012-09-28T16:35:34"
This also raises an exception when the date format is wrong, which is useful to notice when things break.
For more Information see the Apidocs for Date.
Find
(\d+)\/(\d+)\/(\d+) ([\d:]+)
replace with
$3-$1-$2T$4
Here you can see the groups (which in the regex are the parts in ()), $1 is the first group, $2 the second and so on. Basically you need to reorder the groups putting - in the middle and T before the hour.

Resources