I want to get the yesterday date according to the user input.I am having the date value on $date variable.I want to get the previous date of the $date variable.I have seen that previous date can be get by this
date('d.m.Y',strtotime("-1 days"));
But i don't know how to use the $date value here.
Help me to get the previous date of $date variable
$prev_date = date('Y-m-d', strtotime($date .' -1 day'));
View :
Previous
This will decrease the date by one from the date you are on at the time.
ook, Try this code you will definitely Get yesterday date according to user input
$old_date = '21-12-2018';
$date = date('d-m-Y',strtotime($old_date . "-1 days"));
echo $date; // OutPut is: 20-12-2018
$prev_date = date('Y-m-d', strtotime($date["date"] .' -1 days'));
$result2 = $this->db->query("SELECT * FROM daytot WHERE tdate='$prev_date'")->row_array();
Related
How do I make a date that was initially in the format 18-10-2019 to be 181019? If the user registers, then the user will get a token number in the form of the date registered plus 1.
You can achieve it by using date function
$d1 = '18-10-2019';
$d2 = date('dmy', strtotime($d1));
echo $d2; // 181019
You can format it as such;
$date = Carbon::now();
$formattedDate = $date->format('dmY');
I have tried to access date stored in my db table and compare it with current date so that I can get the number of days but it shows this error
DateTime::__construct(): Failed to parse time string ([{"quit_date":null},{"quit_date":null}]) at position 0 ([): Unexpected character
This is the code that use in my controller
$quit_date = Information::select('quit_date')
->where('user_id','=',\Auth::user()->id)
->get();
$date = new Carbon($quit_date);
$now = Carbon::now();
$day = $date->diffInDays($now);
but if I set the $quit_date manually with the date for example "2019-04-25 00:00:00.000000", the code works fine and shows the days different between the dates, but when I use the Information::select to read the date from database, it shows error.
use Auth; //top of controller
$checkdate = Information::
->where('user_id','=',Auth::user()->id)
->first();
$quit_date=$checkdate->quit_date;
$date = new Carbon($quit_date);
$now = Carbon::now();
$day = $date->diffInDays($now);
The issue occurs because you are using ->get() at the end of your query. That method returns a Collection not a single object. The issue is solved by using ->first() to return a single object.
The error itself is because in the line $date = new Carbon($quit_date);, Carbon cannot convert a Collection to a date.
This should work:
$quit_date = Information::select('quit_date')
->where('user_id','=', \Auth::user()->id)
->first(); //Changed this from ->get()
$date = new Carbon($quit_date);
$now = Carbon::now();
$day = $date->diffInDays($now);
I want to set timezone to GMT+8
$data = Carbon::now();
$data->setTimezone(8);
but the result given is
Carbon #1521099609 {#2145
date: 2018-03-15 14:40:09.759487 Asia/Krasnoyarsk (+07:00)
}
I have no idea why it happen, so I must use timezone name to get exact date I want?
Try like this
$data = Carbon::now();
$data->setTimezone('Asia/Krasnoyarsk');
Also make sure to set the timezone else it will have the default value which is set in your php.ini file
Carbon::now(new DateTimeZone('Europe/London'));
Hope this helps
First get the timezone name, then set:
$data = Carbon::now();
$timezoneName = timezone_name_from_abbr("", 8*3600, false);
if($timezoneName)
$data->setTimezone($timezoneName);
I have two date variables $to and $from.
$from = '2016-06-01';
$to = '2016-06-20';
I am using whereBetween to search between the two dates.
If I select for example 1st June to 20th June it only displays records from the 1st to the 19th. How do I include the 20th in that search?
Here is an example of my where clause:
->whereBetween('CompletedDate', array($fromDate, $toDate))
The issue is the SQL server reads these dates like so...
$from = '2016-06-01 00:00:00';
$to = '2016-06-20 00:00:00';
If you want to include the latest date, you need to update these accordingly...
$from = '2016-06-01 00:00:00';
$to = '2016-06-20 23:59:59';
If you are using a datepicker, the same principle applies. Your logic would probably look something like the following...
$fromDate = new DateTime(strtotime('2016-06-01'));
$toDate = new DateTime(strtotime('2016-06-20'));
... ->whereBetween('CompletedDate', array($fromDate->format('Y-m-d 00:00:00'), $toDate->format('Y-m-d 23:59:59')));
Use Carbon
You can work better with dates using Carbon.
I came up with a little snippet for you:
<?php
public function index()
{
$from = Carbon::parse('2016-06-01')->startOfDay();
$to = Carbon::parse('2016-06-20')->endOfDay();
$users = User::whereBetween('created_at', [$from, $to])->get();
}
Carbon Documentation
Don't forget to import Carbon:
use Carbon\Carbon;
My field date (varchar datatype) is in a custom date format it's dd-mm-yyyy.
Example : 01-01-2016
I want to get data between specific dates from a database field date. Let's say input data are stored in variables: startDate & endDate.
I tried with this query but the result are weird.
$query = DB::table('test')->whereBetween('date', array($startDate, $endDate))->get();
I think it fails because I used a custom date format.
How can this be solved?
#updated
let's say i have date like this
29-12-2015
29-12-2015
29-12-2015
30-12-2015
29-12-2015
01-01-2016
06-01-2016
i set $startDate & $endDate like this
$startDate = "01-12-2015";
$endDate = "01-01-2016";
it's even not get any result with this script
$query = DB::table('test')->whereBetween('date', array($startDate, $endDate))->get();
but if I using
$startDate = "01-12-2015";
$endDate = "31-12-2015";
i get all data...which it's wrong result because 2016 data should not in range...it's somehow like not filtered
since your date datatype is varchar you can try to use str_to_date function in mysql then before using $starDate and $endDate variable convert it's format first.
Sample code is like this.
$startDate = date("Y-m-d", strtotime("01-12-2015"));
$endDate = date("Y-m-d", strtotime("31-12-2015"));
$query = DB::table('test')->whereBetween("str_to_date(date, '%d-%m-%Y')", array($startDate, $endDate))->get();
Hope that helps.
The format shouldn't be a problem; Unless the data you POST are in different format from what the database holds. Make sure that the format for the date field in database matches the format to the ones you store in $startDate, $endDate.
Also I would solve this by taking a slightly different approach. This can become a model function, call it getDataBetweenDates(). Each time you need to query the database, to retrieve the data between a specified range of dates, you do a call to this function from the controller:
Model
public function getDataBetweenDates($startDate, $endDate) {
$range = [$startDate, $endDate];
return $this
->whereBetween('date', $range)
->get();
}
Controller
$startDate = Input::get('start_date');
$endDate = Input::get('end_date');
$model = new Model; // use your model name;
$data = $model->getDataBetweenDates($startDate, $endDate);