Convert unix timestamp to normal Date in Json - ajax

I am using laravel 5.0. I am getting data from controller as json. I am having value like this.
{"timstamp_date":"1434360957"},
I need to convert this unix timestamp value as Normal Date Like (15-06-2015) or (15-March-2015).
I have used Date(timstamp_date) but it is showing current time only. Not my timstamp date

You could use:
date("d-m-Y H:i:s", 1434360957);
EDIT
You could try;
var dateTime = new Date(1434360957*1000);
var formatted = dateTime.toGMTString();
https://jsfiddle.net/sp57Lnpf/

Use the date function. You need to specify the format as the first parameter:
date("d-m-Y", $timestamp_date)
http://php.net/manual/en/function.date.php
Laravel also comes with Carbon you could use that if you wanted to for further manipulation of the data if you so required it.
http://carbon.nesbot.com/docs/

Related

Error when using the format: $table->date('column_name') in Laravel

I'm having some trouble with date formats in Laravel.
I have a database migration which has a field that looks like the following:
$table->date('Date');
Then, in the controller, I try to fill this field in with the following content:
2019-04-15
But I get the following error:
invalid datetime format: 1292 Incorrect date value: '"2019-04-15"' for column 'Date' at row 1
Any help on which would be the correct way of declaring the field 'Date' in the database would be apreciated.
Thank you very much in advance.
It's to do with strict date typing in MySQL I believe.
Check these answers for more information.
https://stackoverflow.com/a/47946950/1848660
https://stackoverflow.com/a/48998959/1848660
Try using Carbon to generate dates and timestamps.
This will generate a timestamp for the current date and time.
Carbon\Carbon::now();
Or you can parse your current date and convert it to a Carbon stamp
Carbon\Carbon::parse($date)->toDateTimeString();
Documentation:
https://carbon.nesbot.com/docs/
This works with your case:
$date = "2019-04-15";
$formatted_date = Carbon::createFromTimestamp(strtotime($date))->format('Y-m-d');
You can go ahead and test your insert. I have tested this. You won't face the 1292 Incorrect date ERROR.
DB::table('yourTable')->insert(['date'=> $formatted_date]);
Try with this,
$table->date ? date('Y-m-d', strtotime($table->date)) : '-';
You should convert your date to string and then apply date function on that for different date format.

Set input default date format for dates in a model

There is a way to set the default date format of input dates in a model?
I need to create a model with data received in JSON format and date are formatted as Y-m-dTH:i:sP, but date in the DB are stored in the default Y-m-d H:i:s format. I added all the date fields in the $dates property of the model, but now when i create the model setting all the params in the constructor, like this:
$model = new Model($params);
I get a Carbon conversion error (InvalidArgumentException) because it tries to create a Carbon object from the Y-m-d H:i:s format.
I know Date mutators, but if i set
protected $dateFormat = "Y-m-d\TH:i:sP";
it creates the Carbon object correctly but i get an error while storing in the DB, because it tries to store the record with that format.
Is there a standard way to achieve what I need without converting the date format on each date param?
//date format = Y-m-dTH:i:sP
$date = '2011-09-16T11:38:23-05:00';
$new_date = date_format(date_create($date), 'Y-m-d H:i:s');

Laravel: Check if time + 4 hours has passed

I store in my database a date format like this:
2017-02-22 16:55:40
I added it to my database like this:
Carbon::now();
I need to check if 4 hours passed since this date.
How I can do this? I couldn't figure out how I can convert this format into Carbon or timestamp.
If you are using Laravel and the date is a Carbon instance from a Model you have access to the whole Carbon API.
You can use the Difference API of Carbon for this specific purpose.
echo $model->thedate->diffInHours($now, false);
If your model does not threat the date as a carbon instance you can cast it by adding the date to the dates array of the current model like so
protected $dates = [
'field_name',
];
Check out Date casting for more information
Update with an explicit example
$user = User::first();
// This will return the difference in hours
$user->created_at->diffInHours(Carbon\Carbon::now(), false);
You can convert it to a Carbon object with:
Carbon::parse('2017-02-22 16:55:40');

Laravel Query Builder - Where date is now using carbon

How to get only date on database column, I have difficulties using Carbon on controller:
$data['nowUser'] = User::where('date', Carbon::today()->toDateString())->get();
Date column looks like this in the database:
That is a DATETIME column, so there's no need for additional formatting the Carbon instance. However you need to use whereDate if you want the fetch all users for which the date column contains today's date:
$data['nowUser'] = User::whereDate('date', '=', Carbon::today())->get();
Because when you pass Carbon::today() to the Query Builder method, the __toString method will be automatically called and return a DATETIME string with the format from Carbon::DEFAULT_TO_STRING_FORMAT, which is exactly that MySQL format Y-m-d H:i:s.

Extract only date part in a datetimein LINQ

We have some data in a DataTable and we are using the query like this to get what we need.
IEnumerable<Results> subResult = from query in datatable.AsEnumerable()
select new Results
{
Name = query.Field<string>("Name"),
Date = query.Field<DateTime?>("Date")
}
This above query returns what i need but date in full format (Ex: m/dd/yyyy hh:min:sec am/pm) but we need only date part of it(only mm/dd/yyyy need to be pulled in). When looked in the properties of this, couldn't find an implicit way to get it, please help me in getting this result. Thanks.
The DateTime class has all the properties you just mentioned. You should try to fix your display with the ToString("anyformatyouwant") formats.
What about this?
Date = query.Field<DateTime?>("Date").Date
This will give you just the date part:
IEnumerable<Results> sub-result= from query in datatable.AsEnumerable()
where new Results
{
Name = query.Field<string>("Name"),
Date = query.Field<DateTime?>("Date").Date
}
However if you just want to display the date somewhere (meaning you're not using this to do date grouping, etc. then I would just specify the display format in your UI layer.
AFAIK there is no Date Class in .net, if you want pure date you should write a Class for that, otherwise you can pull out the part as a string
DateTime.Now.Date.ToString("yyyy/MM/dd")
gives you the current date in string type, but if you get the .Date part it gives you a DateTime object which still has a time part showing AM 12:00:00:0 blah blah

Resources