analysis this datetime result and export just date in laravel - php

this is a function to convert date
private function convertShamseToMiladi($request){
$startdate = \Morilog\Jalali\CalendarUtils::convertNumbers($request, true);
$sdate = \Morilog\Jalali\CalendarUtils::createDatetimeFromFormat('Y-m-d' , $startdate);
return $sdate;
}
when i use this function
$this->convertShamseToMiladi($request->startdate);
the result i see is same as this part:
DateTime #1577133000 {#273 ▼
date: 2019-12-24 00:00:00.0 asia/tehran (+03:30)
}
and i need just this part : 2019-12-24
and i want to know when i dd() the date request why show me this kind of result?

Your convertShamseToMiladi() method is returning php \DateTime, so you can use format to get desired result:
$this->convertShamseToMiladi($request->startdate)->format('Y-m-d');
Or if you want your method to always return formatted date you may change it like this:
private function convertShamseToMiladi($request){
$startdate = \Morilog\Jalali\CalendarUtils::convertNumbers($request, true);
$sdate = \Morilog\Jalali\CalendarUtils::createDatetimeFromFormat('Y-m-d' , $startdate);
return $sdate->format('Y-m-d');
}

I suggest You can use carbon https://carbon.nesbot.com/docs/ instead of morilog/jalali
You can change format of date in any other format using carbon
$date = '12-12-2017 23:23:34';
echo Carbon::parse($date)->format('Y/m/d');
You can change the format as you want. https://carbon.nesbot.com/docs/#api-formatting

Related

Date Format in Laravel Project

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;

get date from dateTime in PHP

I want to extract date without time from a single value from array result thaht I got.
I tried using array_slice but it didn't work.
My code..
$dateRows = $this->get('app')->getDates();
$dateRow = json_decode(json_encode($dateRows[0], true));
dump($dateRow[0]);die;
And I got result..
"2014-01-01 00:00:00"
And I want it to return just
"2014-01-01"
Very Simple, just use date_create() on your date & then format it using date_format() as follows -
$date = date_create("2014-01-01 00:00:00");
echo date_format($date,"Y-m-d");
So, in your case, it would be something like -
$date1 = date_create($dateRows[0]);
echo date_format($date1,"Y-m-d");
you can use strtotime function as well for getting formatted data
$a = "2014-01-01 00:00:00";
echo date('Y-m-d', strtotime($a));

Getting date from a string without time in php

I have to convert a string input to date "1990/07/22" to 22/07/1990 as date,My function is as follows as:
public function($date){
$date_format_sec = strtotime($date);
$date_of_birth = new \DateTime('#'.$date_format_sec);
}
It uploads date as 21/07/1990 since I didn't give time.How to get the exact date given as same as input.
You can format your date with php
$formatedDate = $date_of_birth->format('d-m-Y');
Documentation
$input = '1990/07/22';
$date = \DateTime::createFromFormat('Y/m/d', $input)->format('d-m-Y');
As I said, you don't need to use strtotime, if the $date string is a valid date, the class DateTime will read it. After that you can use format.
In this example I set the format to be always the one you expect, while you can put any other format accepted by it.
public function getMyDate($date, $format = 'd/m/Y') {
$date_of_birth = new \DateTime($date);
return $date_of_birth->format($format);
}
public function formatDate($date) {return date('d/m/Y', strtotime($date));}

How to use type DATE with laravel php

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

Carbon subtracting Datetime from Carbon::now()

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');

Categories