I'm moving data from the old project to my laravel project the start date format was in this format 1496242424 ,1496269010 How I convert it to time
To a Carbon object which can be saved on your models, if you add the field to the $dates property.
Carbon\Carbon::createFromTimestamp(1496242424);
$fdate=$request->Fdate;
$tdate=$request->Tdate;
$start = Carbon::parse($fdate)->format('Y/m/d');
$end = Carbon::parse($tdate)->format('Y/m/d');
$days = $end->diffInDays($start);
echo $days;
exit;
you can convert like this
<?php
echo $currentDate = date('Y-m-d H:i:s', 1496242424);
?>
output :
2017-05-31 16:53:44
It is very simple use date function in php To convert this to time use below code
echo date('Y-m-d',1496242424);
output of above code will be
2017-05-31
and to convert this to datetime use below code:
echo date('Y-m-d h:i:s',1496242424);
output of above code will be
2017-05-31 02:53:44
Thanks
why convert it to dateTime. keep it as unix timestamp. Just add a method in your model to tell it to use unix timestamp
class User extends Model
{
protected $dateFormat = 'U';
}
Use this code in your datepicker and the format is changed as per your own choice
$(function() {
$(".datepicker").datepicker({
dateFormat: "dd/mm/yy",
Set this line in your controller
$post->start_date =Carbon::parse(strtotime($request->start_date))->format('Y-m-d');
You should use carbon library use Carbon/Carbon;
Related
I need to insert Timestamp in MySQL from input in Laravel application
data coming from my input is in this format 12/27/2017 MM/DD/YYYY how can I convert it into this format Y-m-d H:i:s
any alternatives are highly appreciated like pass data from the input which input we use so, I can get data in timestamp format.
and at the end, I need to sort data order by date
If you want to do it in Laravel way you can use mutator and Carbon. This is an example of model:
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model
class Post extends Model {
protected $dates = ['date'];
public function setDateAttribute($value)
{
$this->attributes['date'] = Carbon::createFromFormat('d/m/Y', $value);
}
}
Now when you update or create post date attribute will be automatically converted. So you can save it like this:
$post = new Post();
$post->date = '16/12/2017';
$post->save();
You can use DateTime:
$inputDate = \DateTime::createFromFormat('m/d/Y', '12/27/2017');
$formatedDate = $inputDate->format('Y-m-d H:i:s');
As for me I done date conversion in this way, for example to making invoices. I hope this can be done by PHP.
$input_date = "12/15/2017"; // input in MM/DD/YYYY
$output_date = date('Y-m-d H:i:s',strtotime($input_date)); //set output to 2017-12-15 00:00:00
echo $output_date; //produce output
It generates the following result
2017-12-15 00:00:00
I hope this will work. If you want only date, you can omit H:i:s based on your purpose. Thank you.
$date=date_create("12/27/2017");
$rawDate = date_format($date,"Y/m/d");// Produced - 2017/12/27
echo str_replace('/','-',$rawDate ); // Output - 2017-12-27
to add H:i:s, if there is no time just add 00:00:00 at the end of date begins with space.
If you're dealing with created_at or updated_at which Laravel create for every table, you must add 00:00:00 to end of date to get all data begins with that respective date
This solution worked for me if we want the format of date from input is different from the format the timestamp accepts
Laravel way of doing is
protected $fillable = ['organization_name', 'job_apply_link', 'job_description', 'city_id', 'province_id', 'sector_id', 'image_file', 'test_id', 'newspaper_id', 'catagory_id', 'slug', 'meta_keywords', 'meta_description', 'job_title', 'created_at'];
public function setCreatedAtAttribute($value)
{
$inputDate = \DateTime::createFromFormat('m/d/Y', $value);
$formatedDate = $inputDate->format('Y-m-d H:i:s');
$this->attributes['created_at'] = $formatedDate;
}
I'm very new using Laravel and I have a problem with a column that I created in the migrations. I have a column that is type date. My problem is that I don't know how can I manage it, how do I have to store some data for a form, etc.
I was asking if someone could tell me, in a method of a controller with the parameter Resquest $r, how can a convert the string from that form in a variable of date. I've been searching and I can't find anything that could help me. Thanks in advance!
Edit:
This is one method of my controller:
public function addObjectives(Request $r,$id){
$gobjectives = Mgobjective::all();
$sobjectives = Msobjective::all();
$mprogram = Mprogram::find($id);
$a = Area::find($mprogram->marea_id);
if($a==null){
$area = new Area();
$area->id = $mprogram->marea_id;
$area->saveArea();
}
$p = Program::find($id);
if($p==null){
$program = new Program();
$program->id = $id;
$program->initialDate= "";
$program->finalDate= "";
$program->frequency= "";
$program->area_id = $mprogram->marea_id;
$program->saveProgram();
}
}
In initialDate y finalDate de program, I have an empty string because before it was a string. I would like to put there a date that doesn't have to be the actual date.
Use date() function like:
$date = date('Y-m-d', strtotime('your date'));
Explanation: Y-m-d is the default date format for mysql date type. So what ever formated date you have, convert it into Y-m-d.
<?php
namespace App\Http\Controllers;
class YourController extends Controller
{
public function yourMethod(Request $request)
{
// If you use a HTML date element, you "should" receive the data in yyyy-mm-dd
// format which can be understood by DateTime's constructor
$date = new \DateTime($request->input('date'));
// If you use a different input or expect a date in a different format you can
// use the createFromFormat method, where your first parameter is the date
// format you expect and the second is the date input.
$date = \DateTime::createFromFormat('d/m/Y', $request->input('date'));
// Once your date has been loaded into a DateTime object, you can use the
// format method to output the date in any format you like. MySQL and other
// databases tend to expect and store dates in yyyy-mm-dd format.
echo $date->format('Y-m-d');
}
}
Try using Carbon
Carbon::createFromDate($year, $month, $day, $tz);
or
Carbon::createFromFormat('Y-m-d H', '1975-05-21 22')->toDateTimeString(); // 1975-05-21 22:00:00
Take a look at the documentation
I couldn't find anywhere in documentation how to show current year or month with Carbon?
when i write this:
Carbon\Carbon::now('m');
it gives me the whole time stamp, but I just need the month
like
date('m');
but it must be Carbon!
How can I achieve this?
$now = Carbon::now();
echo $now->year;
echo $now->month;
echo $now->weekOfYear;
Update:
even this works since Laravel 5.5^
echo now()->month
I think you've already worked this out in a comment, but just for clarity: Carbon extends PHP's native DateTime class, so you can use any of the methods available on that, like format:
Carbon::now()->format('M');
(where M is the modifier for A short textual representation of a month, three letters)
You can use these both ways to get the current month
Carbon::now()->month;
or
Carbon::now()->format('m');
Just use this in your any blade file for print year:
{{ \Carbon\Carbon::now()->year }}
I wanted to get the current month and got to this question, to get the current month:
$now = Carbon::now();
$monthStart = $now->startOfMonth();
w/ Carbon + Laravel:
now()->format('M')
use Carbon\Carbon;
$today= Carbon::now();
echo $today->year;
echo $today->month;
echo $today->day;
echo $today->hour;
echo $today->minute;
echo $today->second;
Laravel 8
return date('m');
or
return now()->format('m');
You cant call it statically
use
$now = Carbon::now();
echo $now->month
I want to use this Carbon function:
Carbon::now()->subDays(5)->diffForHumans()
And I need to create the correct integer.
I am loading a string with a Datetime, which I want to subtract in Laravel like this:
$datetime = $score->created_at;
Then I save the current Time into a variable
$now = Carbon::now()->toDateTimeString();
This is what I get:
echo $now . '<br>'; // 2014-07-13 22:53:03
echo $datetime; // 2014-07-12 14:32:17
But when I want to subtract one from another I get the following error:
echo $now - $datetime;
Object of class Carbon\Carbon could not be converted to int
Any help here would be greatly apreciated.
I know it's a bit late, but this works:
$score->created_at->diffForHumans(\Carbon\Carbon::now())
If you want to change the date format just use the format function
$now = Carbon::now();
$score->created_at->diffForHumans($now)->format('Y-m-d');
How to get current date in codeigniter in YY-mm-dd format. I wants to get current date in YY-mm-dd frmat and put this value into input text box
You can use the PHP date function.
date('Y-m-d');
Up to my knowledge, there is no separate date function in codeigniter.
EDIT :
But if you want date in this format 13-04-05 [ yy-mm-dd ], Try this
date('y-m-d');
For more date formats, check this link PHP Date Formats
Try to use this is a generic format of DateTime
echo date('Y-m-d H:i:s');
use php date function
echo date("Y-m-d");
will give you the result
What about:
$date = new \Datetime('now');
var_dump($date);
if you want the full date:
echo date('Y-m-d');
depends your date structure.
echo date('d-m-Y');
if you want year only, then echo date('Y'); is enough
Use php date() function, like this
echo Date('Y/m/d');
it give you the desired result!