A non well formed numeric value encountered when converting DateTime - php

I am using the fullCalendar plugin/directive in Angular, and I am currently having an issue when trying to save the date/time into my database.
These are the values being posted to my server:
{"title":"Hey","start":"2015-08-13T00:00:00.000Z","end":"2015-08-13T00:00:00.000Z","allDay":true}
Now in my controller I try to convert both date/time string into valid date/time format before saving into my database:
public function store(ScheduleRequest $request)
{
$schedule = new Schedules;
$schedule->allDay = $request->allDay;
$schedule->start = strtotime(date('Y-m-d H:i:s', $request->start));
$schedule->end = strtotime(date('Y-m-d H:i:s', $request->end));
$schedule->title = $request->title;
if ($schedule->save())
{
return [
'success' => 'Data Was Saved Successfully'
];
}
}
This is the error I get:
A non well formed numeric value encountered
I would like to know how to convert both datetime values into valid datetime objects in PHP using the specified format.

strtotime is converting a string into a timestamp and date is converting a timestamp into a string, you need to reverse date with strtotime like so:
public function store(ScheduleRequest $request)
{
$schedule = new Schedules;
$schedule->allDay = $request->allDay;
$schedule->start = date('Y-m-d H:i:s', strtotime($request->start));
$schedule->end = date('Y-m-d H:i:s', strtotime($request->end));
$schedule->title = $request->title;
if ($schedule->save())
{
return [
'success' => 'Data Was Saved Successfully'
];
}
}

Edit: Sorry, strtotime doesn't do what I thought it did, looks like you want DateTime::createFromFormat to create a DateTime object from a String and then you can go to a unix timestamp from there.

Adding strtotime($mydateValue) fixed it for me.

Related

PHP DateTime failing to convert Amazon shipping date

I've realised my DateTime function is not converting and returning false.
Example date/time as provided by Amazon. This is what I am trying to convert from:
2020-07-28T23:00:00Z
Function which accepts the date as provided by Amazon and tries to convert it.
protected function dateUtcToEuropeLondon($date)
{
try {
$dt = new DateTime($date, new DateTimeZone('UTC'));
$dt->setTimezone(new DateTimeZone('Europe/London'));
return $dt;
} catch (Exception $e) {
return false;
}
}
Full function.
// Detect if custom shipping date exists
$date = (isset($order_details->EarliestShipDate)) ? $order_details->EarliestShipDate : '';
// Set custom meta field so we can easily access it via WC_Order
if (!empty($date)) {
$date = $this->dateUtcToEuropeLondon(strtotime($date));
if ($date instanceof DateTime) {
update_post_meta($order_id, '_wpl_shipping_date', $date->format('Y-m-d\TH:i:s'));
}
}
For starters, I checked what value I got back from:
$date = $this->dateUtcToEuropeLondon(strtotime('2020-07-28T23:00:00Z'));
it was nothing: ''.
The problem is that you're trying to create a DateTime object using a UNIX timestamp (via strtotime) which is all numbers. What DateTime is expecting is the more 'human friendly' d/m/y style format - so you'll need to specify that what you're giving it is a 'unixtime' string, rather than a 'datetime' string -- subtle difference.
Try changing
$dt = new DateTime($date, new DateTimeZone('UTC'));
to
$dt = new DateTime('#'.$date, new DateTimeZone('UTC'));
Using an # at the start of the string will tell your DateTime object that you're sending in a unix timestamp. Once it's happy, it will then allow your function to determine the correct timezone from UTC to Europe/London (GMT/BST).

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 enter time-stamps in mysql from an input

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;
}

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

Laravel Carbon Data Missing

In my model I have the following:
protected $dates = [
'start',
'end',
'created_at',
'updated_at'
];
I am using a datetime picker to insert the start and end dates, in this format:
2016-01-23 22:00
Without the seconds. When I do it like this, I get this error:
InvalidArgumentException in Carbon.php line 425:
Data missing
at Carbon::createFromFormat('Y-m-d H:i:s', '2016-01-23 22:00') in Model.php line 3015
If I do include the seconds, it works. The seconds are not important to me, and I do not want to include them in my datetime picker fields. Any way around this so I can still use those fields as date fields?
tl;dr
Your date string and your date format is different, you have to change the format string or modify the date string so they match.
Explanation
The Problem
This error arises when Carbon's createFromFormat function receieves a date string that doesn't match the passed format string. More precisely this comes from the DateTime::createFromFormat function, because Carbon just calls that:
public static function createFromFormat($format, $time, $tz = null)
{
if ($tz !== null) {
$dt = parent::createFromFormat($format, $time, static::safeCreateDateTimeZone($tz));
} else {
$dt = parent::createFromFormat($format, $time); // Where the error happens.
}
if ($dt instanceof DateTime) {
return static::instance($dt);
}
$errors = static::getLastErrors();
throw new InvalidArgumentException(implode(PHP_EOL, $errors['errors'])); // Where the exception was thrown.
}
Not enough data
If your date string is "shorter" than the format string like in this case:
Carbon::createFromFormat('Y-m-d H:i:s', '2017-01-04 00:52');
Carbon will throw:
InvalidArgumentException in Carbon.php line 425:
Data missing
Too much data
If your date string is "longer" than the format string like in this case:
Carbon::createFromFormat('Y-m-d H:i', '2017-01-02 00:27:00');
Carbon will throw:
InvalidArgumentException in Carbon.php line 425:
Trailing data
Under the hood
According to the documentation on mutators the default date format is: 'Y-m-d H:i:s'. The date processing happens in the Model's asDateTime function. In the last condition the getDateFormat function is called, thats where the custom format comes from. The default format is defined in the Database's Grammar class.
Solution
You have to make sure that the date string matches the format string.
Change the format string
You can override the default format string like this:
class Event extends Model {
protected $dateFormat = 'Y-m-d H:i';
}
There is two problem with this approach:
This will apply to every field defined in the model's $dates array.
You have to store the data in this format in the database.
Edit and format the date strings
My recommended solution is that the date format should stay the default 'Y-m-d H:i:s' and you should complete the missing parts of the date, like this:
public function store(Request $request) {
$requestData = $request->all();
$requestData['start_time'] .= ':00';
$requestData['end_time'] .= ':00';
$event = new Event($requestData);
$event->save();
}
And when you want to use the date you should format it:
public function show(Request request, $eventId) {
$event = Event::findOrFail($eventId);
$startTime = $event->start_time->format('Y-m-d H:i');
$endTime = $event->end_time->format('Y-m-d H:i');
}
Of course the fields should be mutated to dates:
class Event extends Model {
protected $dates = [
'start_time',
'end_time',
'created_at',
'updated_at',
'deleted_at',
];
}
Models
This function disabled, the emulations for carbon in Datetimes
https://laravel.com/docs/5.0/eloquent#date-mutators
public function getDates()
{
return [];
}
You can set the $dateFormat in your model as Christian says, but if you don't want to imply the updated_at and created_at fields into the operation you can use events to "correct" the datetime object before saving it into the database.
Here you have the official doc about it: https://laravel.com/docs/5.2/eloquent#events
You need to set protected $dateFormat to 'Y-m-d H:i' in your model, see https://laravel.com/docs/5.2/eloquent-mutators#date-mutators
Make sure you are not omitting the "created_at" or "updated_at" fields in some rows in your database, which are required; if that is the case delete the records where they have those empty fields or enter a value in the valid timestamp format, example '2018-09-01 15:18:53'.
This is one possibility
You need to check the column of resultant data. If your column name 'created_at' or 'updated_at' is null, then it will through this error.
How to Solve ?
First of all, you need to store the data in those two columns using Laravel carbon.
Eg:
$user = new User();
$user->created_at = Carbon::now()->setTime(0,0)->format('Y-m-d H:i:s');
$user->updated_at = Carbon::now()->setTime(0,0)->format('Y-m-d H:i:s');
$user->save();
That's All, I hope it will work
Happy Coding....
For me the problem was with SQLServerGrammar located into the vendor (vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\SqlServerGrammar.php).
Default in SQLServerGramma is Y-m-d H:i:s.v.
We extended the class end removed .v.

Categories