I have made a mistake by storing the date as VARCHAR in the database and here it looks like 02/12/2018 and I have created a variable using carbon to get the current date +14 days.
$current_date_plus_14 = Carbon::now() -> addDay(14) -> format('d/m/Y');
Problem is
I am trying to compare this date 02/12/2018 which is in the database and stores as VARCHAR to the date that I have generated using Carbon which is 09/04/2017.
Eloquent Code
$gquery = Client::where('required_date', '>=', $current_date_plus_14) -> get();
What I get
It doesn't get any results because it compare only the day in the required_date and the day in the carbon date.
While it should return value because the there is more than 1 year difference?
Try this function that converts date as String (May work with varchar) to a real date
private function convertDateString($date)
{
if (is_string($date)) {
$date = Carbon::parse($date,new DateTimeZone('YOUR_DATE_TIME_ZONE'));
}
return $date;
}
Even if your field is a VARCHAR instead of DATETIME or DATE, you can still mark it as a date in your Eloquent model by adding the attribute name to $dates. You'll also have to change your model's $dateFormat since you have a non-Y-m-d H:i:s format.
Related
i fetched a number of days from the database to add to an inputed date from my form but it keeps giving a wrong output as date 1970-01-01
$newTime = date('d-m-Y', strtotime($all_leave->date_from.' + '.$leaveDays.' days'));
and for my $leaveDays
$leaveDays = leaveType::all()->where('leave_type','=',$all_leave->leave_type)->pluck('leave_days');
The pluck method retrieves all of the values for a given key. So return array of all values not single value but
strtotime() needs days in number and you are providing array. So it is giving you wrong date. So you need to get first value from array as leavedays .
Just be sure that for leave days you will be only one value, if there are multiple values then you have to iterate if you needed else it will take only first value.
Try this
$leaveDays = leaveType::all()->where('leave_type','=',$all_leave->leave_type)->pluck('leave_days');
$leaveDays = empty($leaveDays[0]) ? 0 : $leaveDays[0];
$newTime = date('d-m-Y', strtotime($all_leave->date_from.' + '.$leaveDays.' days'));
Use Carbon instead of directly data object
// $all_leave->date_from should be in standard mysql datetime format 2012-01-31 00:00:00
$newTime = Carbon::parse($all_leave->date_from)->addDays($leaveDays)->format('d-m-Y');
I want to get date from VARCHAR column.
(eg: 4/14/2018 12:00:00 AM)
How do I display only date
(eg: 4/14/2018)?
SELECT date(created_at) from self_balance
here created_at(varchar)
this returns NULL value
You can run this query to get your output,
SELECT DATE_FORMAT(STR_TO_DATE(created_at, "%Y-%m-%d"), "%Y-%m-%d") FROM
self_balance
First I am matching date format and converting it to date and then formatting.
You can fetch date like a normal string from the database then you need to use strtotime which parses an English textual DateTime into a Unix timestamp. Then you can use
date function which returns the formatted date string. I have passed a static string. You can pass your string variable which you are fetching from the database
$time = strtotime($date_string_from_database);
<?php
$time = strtotime('4/14/2018 12:00:00 AM');
$newformat = date('m/d/Y',$time);
echo $newformat;
?>
You can see the live demo here
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 am using DateTime function of php. I get a date from a calendar in format d-m-Y and pass it via ajax to my function. I am getting the date right till this step.
When I try to store the date in unix format using:
$ai_ff_date=DateTime::CreateFromFormat('d-m-Y', $data['date']);
$final_date=$ai_ff_date->format('U');
The date stored is wrong. Suppose the date I passed via ajax is 26-12-2016 then in database 27-12-2016 is stored. Why its counting one more day then the input.
use this code :
$date = date('Y-m-d H:i:s', strtotime('-1 day', $stop_date));
$ai_ff_date=DateTime::CreateFromFormat('d-m-Y',$date);
$final_date=$ai_ff_date->format('U');
and please check the variable (code not tested)
You might want to convert the Date-Format to "Y-m-d" First and then call-in the DateTime() Constructor. However, since what you are trying to do is just get the TimeStamp you might also do that directly without using DateTime. The Snippet below shows what is meant here:
<?php
$data = ['date'=>"13-12-2016"]; //<== JUST AN EXAMPLE FOR TESTING!!!
// SIMPLY CONVERT THE DATE TO Y-m-d FIRST.
$dateYMD = date("Y-m-d", strtotime($data['date']));
// THEN USE DateTime CONSTRUCTOR TO CREATE A NEW DateTime INSTANCE
// AND THEN RUN THE FORMAT YOU WISH::
$final_date = (new DateTime($dateYMD))->format('U');
var_dump($final_date); //<== YIELDS: string '1481583600' (length=10)
var_dump(date("Y-m-d", $final_date)); //<== YIELDS: string '2016-12-13' (length=10)
I have a timestamp variable column in a mysql database. Trying to convert a carbon timestamp to something that I can input there, but Carbon::now() only returns a Carbon object and when I try to use the timestamp string of the Carbon object, it does not register in mysql.
public function store(CreateArticleRequest $request){
$input = $request->all();
var_dump($input); // JUST SO YOU CAN SEE
$input['published_at'] = Carbon::now();
var_dump($input); // JUST SO YOU CAN SEE
Article::create($input);
}
My first var dump is like so:
array (size=4)
'_token' => string 'Wy67a4hWxrnfiGz61wmXfYCSjAdldv26wOJiLWNc' (length=40)
'title' => string 'ASDFasdf' (length=8)
'body' => string 'asdfasdf' (length=8)
'published_at' => string '2015-08-26' (length=10)
My second var dump is like so.
The mysql column relating to "published_at" is a timestamp variable. How an I suppose to convert this from a Carbon Object?
Thanks in advance.
The short answer is that toDateTimeString() is what you're looking for:
$input['published_at'] = Carbon::now()->toDateTimeString();
See http://carbon.nesbot.com/docs/ for more options, including toDateString() if you just want the date part and not the time.
But an even better way to handle it would be to let Laravel handle casting the date value to/from a Carbon object for you. See https://laravel.com/docs/5.4/eloquent-mutators#date-mutators.
The problem is in your date string, for example, you have this:
public function setCrbDateAttribute($value)
{
$this->attributes['crb_date'] = \Carbon\Carbon::createFromFormat('d-m-Y h:i', $value);
}
Now, if there is a date like 10-12-2014 then this error will occur because the hour and minute is missing. So you make sure that the date contains all the pars and also make sure that the date string contains - as a separator not /.
In other words, check the $value before you use Carbon and make sure your date string contains exactly the same formatted string you've used in the method.
This also happens in an accessor method, so check the date value first before you use it in Carbon::createFromFormat().
If you are getting the date from user input then validate the date before using it using date or date_format:format rule, check the validation here.
Answer ref:
Laravel/Carbon Timestamp 0000-00-00 00:00:00 or Unexpected data found. Unexpected data found. Data missing
You can also set Mutator on your model.
public function setPublishedAt($value)
{
$this->attributes['published_at'] = strtotime($value);
}
to convert to timestamp
$model -> setPublishedAt('2015-08-26'); // 1440572400
or you can just convert the date to timestamp using strtotime
strtotime — Parse about any English textual datetime description into a Unix timestamp
Hope this help.