in my date form input i will select only month and year but when i insert into database its saving month and year with current date. but i want set date 1st of slected month im using carbon also
{!! Form::text('vessel_date',null,['class' => 'date-own form-control' , 'id'=>'date-own-id']) !!}
Controller :
$input['vessel_date'] = Carbon::createFromFormat('m-Y', $request->vessel_date)->toDateString();
JS :
$('.date-own').datepicker({
minViewMode: 1,
format: 'mm-yyyy'
});
Here is an example of how I have used Carbon to get the first of the month in a recent application:
// (year, month, day)
Carbon::createFromDate(null, null, 1)
Year, Month, and Day being integers.
To format before insert/update (depending on target DB format) ->format('M d Y')
you can try this. you can find it here.
$('.datepicker').datepicker('update');
$('.datepicker').datepicker('update', '2011-03-05');
$('.datepicker').datepicker('update', new Date(2011, 2, 5));
Just to add to the answer above my comment, also kindly check your date format of your DBMS (e.g if it is same with your datepicker).
answer is
$('.date-own').datepicker({
minViewMode: 1,
format: 'mm-yyyy'
});
Related
I have a date list in the database and from the list, I get the month and despite lay in the dropdown. Right now it works but I don't know how to order the date by ascending before I change the format. Below is the date list:
date
2021-08-02
2021-09-30
2021-08-01
2021-09-06
2021-11-05
Right now in the dropdown display
August
November
September
It should be
August
September
November
Here's my eloquent to display the dropdown
$date = DateList::where('id',$id->id)->select(DB::raw("(DATE_FORMAT(date, '%M')) as newDate"))->groupBy('newDate')->get();
I have a search around but still could not find the answer on how to get the correct way to order the date before changing it to the format. Hope anyone can help.
Why don't you use laravel's awesome collection api?
DateList::where('id',$id->id)
->orderBy('date', 'ASC')
// uncomment the below line if you're concerned with performance
// ->select('date')
->pluck('date')
->map(function($date) {
return $date->format("F");
})
->unique();
Hey GarfieldJuniorI couldn't think any way to implement the above but the way I would suggest is to fetch month's number instead of name and sort it then map the array/Collection to Months name,
for mapping thing either you can user index array of like
$monthNames=[
1=>"JANUARY",
2=>"FABRUARY
]
OR
you can use by creating a dummy date by months Number using date() method of php, It takes format type of string as first argument and second $timestamp
$format ='F' ,
and
$timestamp=mktime(0,0,0,$monthNumber)
ref mktime
so in short your if you get sorted months number Array like this (I'm using date method not indexed Array)
$monthsArray=[1,2,3];
$monthNames=array_map(
function($monthNumber){
return date("F", mktime(0, 0, 0, $monthNumber));
}
,$monthsArray);
your output will look like this
[
"January",
"February",
"March",
]
You can do order by from your query like this
$date = DateList::where('id',$id->id)
->select(DB::raw("(DATE_FORMAT(date, '%M')) as newDate"))
->groupBy('newDate')
->orderBy('newDate', 'ASC')
->get();
I want the nearest date passed from given date using carbon
$givenDate = "2020-07-18";
Today is = 2020-12-01
So the result should be the 18th last month. $result = "2020-11-18"
you can use subMonth() from now to get the last month then create a new datetime with suitable values:
$givenDate =Carbon::parse( "2020-12-20");
if($givenDate>Carbon::now())
$result=Carbon::create(Carbon::now()->year,Carbon::now()->month,$givenDate->day)
->format('yy-m-d');
else
$result=Carbon::create(Carbon::now()->year,Carbon::now()->subMonth()->month,$givenDate->day)
->format('yy-m-d');
I have a table transaction, inside there is column order_on_sale with default value is 0. Then i also have a table config, inside there is column name with two value that is sale_start_date and sale_finish_date. And there are also time columns with values 2018-05-1 18:00 and 2018-05-31 18:00 (YYYY-MM-DD HH: mm).
name and time columns contained in the config table are interconnected,
sale_start_date = 2018-05-1 18:00
sale_finish_date = 2018-05-31 18:00
then when someone orders on sale_start_date and sale_finish_date, the order_on_sale column contained in transaction table will change its value to 1
how do i get the current current time (YYYY-MM-DD HH: mm) to make the change?
$transaksi = new Transaction;
$order_on_sale = 0;
if (Config::get('sale_start_date') && Config::get('sale_finish_date')) {
$order_on_sale = 1;
}
$transaksi->order_on_sale = $order_on_sale;
$transaksi->save();
Below is my code, but I am confused how to get the current date and time if I write such code
thanks for the answers you provide
A bit late answer for questioner but maybe can help someone:
$date = Carbon::now();
$formatedDate = $date->format('Y-m-d H:i:s');
echo($formatedDate);
use the Carbon Library which comes by default with laravel
$date = Carbon::now();// will get you the current date, time
dd($date->format("Y-M-D H:m")); //this will dump the date time in the desired format
Maybe with:
use Carbon\Carbon;
$Now = Carbon::now(new \DateTimeZone('My/TimeZoneifRequired'))->toDateTimeString();
So have you tried using Carbon Class ? as far as i know Laravel 4 supports it.
you can get your current date by doing
$now = Carbon::now()->toDateTimeString();
This will retrieve the current date in the following format YYYY-MM-DD HH:mm:ss (as default)
if you want to get the current date in your mentioned format YYYY-MM-DD HH:mm
by wrapping around Carbon and adding some php functionality you can get so :
$currentTime = Carbon::now()->toTimeString();
$now = Carbon::now()->toDateString() .' '. substr($currentTime, 0, strrpos( $currentTime, ':') ) ;
For further explanation go for the docs : https://carbon.nesbot.com/docs/
simply you can use the below code on the view page.
<p>{{ date('Y-m-d') }}</p>
note: HTML <p> tag is optional.
with hours and minutes
<p>{{ date('Y-m-d H:m') }}</p>
I have a date as 2010-12-24.
I need to store the date as 2010-12-01 in database table. This should be applicable for all dates.
When inserting another date 2010-12-25, it should also be stored as 2010-12-01, and for 2017-01-29, it be 2017-01-01.
How can I set the day to default 1?
I hope this is clear. Any help is appreciated. Thanks.
A much simpler way is to use PHP's built-in date functions :
// here's your date
$date='2017-01-12';
// and here we transform it as we want
$date_first = date('Y-m-01', strtotime($date));
// check out the result :)
echo $date_first;
it can be done in mysql just fine, though.
set #date = '2017-12-25';
select str_to_date(CONCAT(YEAR(#date),' ',MONTH(#date),' 1'), '%Y %m %d');
You don't need to use the #date variable, just replace #date with the date you are need to insert and use str_to_date to replace the day with 1.
You can achieve it directly in MySql as follows:
SELECT DATE_SUB('2010-12-25', INTERVAL DAY('2010-12-25')-1 DAY)
Explanation:
1. DAY('2010-12-25')-1 DAY // will return 24
2. DATE_SUB('2010-12-25', INTERVAL 24) // will return 25-24 = 01 i.e. 2010-12-01
This will work in all cases.
This should work, adjust to your requirement
$today = date("Y-m", strtotime("2017-12-25"));
$dbDate = date ("Y-m-d", strtotime($today."-1"));
echo $dbDate;
Pass $dbDate to the query.
<?php
$date='2017-01-12';
$date=explode('-',$date);
$date[2]=str_replace($date[2],'01',$date[2]);
$date=implode('-',$date);
print_r($date);
And the output is :
2017-01-01
So it will always replace the day with 01.
After you can execute the insert query for the new date as it is already modified in the php.
I am working on a booking system and need to select only the records where a certain field is for today. Not today and in the future, but only today.
The query currently is:
$advancedBookings = Booking::where('type', 2)
->whereNull('estimated_booking_time')
->where('requested_booking_time', ?????);
$advancedBookings->get();
The requested_booking_time is the field that I wish to be checked, the date stored is in this format:
2017-08-23 08:00:00
So I want to only select rows that are on the same day as the current day.
As i understand, you want that records which is created today; then just get the today's date.
$today = date("Y-m-d");
now your query is like this.
$advancedBookings = Booking::where('type', 2)
->whereNull('estimated_booking_time')
->where('requested_booking_time', $today)->get();
You can use whereDate and format your dates with Carbon:
$advancedBookings = Booking::where('type', 2)
->whereNull('estimated_booking_time')
->whereDate('requested_booking_time', '=' , Carbon::today()->toDateString());
$advancedBookings->get();
Or format the your date before the query with Carbon.
You should try this:
$advancedBookings = Booking::where('type', 2)
->whereNull('estimated_booking_time')
->whereDate('requested_booking_time', '=', date('Y-m-d'))
->get();