Hi @Akeno, try with this function:
$carbon = Carbon::instance($dateTime);
Carbon::createFromFormat('y-m-d', $carbon);
Hope it helps you.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi,
so I've got this code:
$newslet = new Newslet();
$newslet->object_id = $object_id;
$newslet->release_time = Carbon::create(2010,10,10,10,10,10);
// first dd($newslet->release_time);
$newslet->importance = $importance;
$newslet->content = $content;
$newslet->save();
// second dd($newslet->release_time);
return $newslet;
If I die dump directly after I created the variable in newslet, it gives me the correct date (this date is just for testing purposes).
But if I let this script run the db table does not contain the release_time I set but the current date time (that one that's used for the created_at column) in the column 'release_time'.
I have no idea why.
Also I noticed that the second die dump is not reached by the script. Though the save()-method returns true (checked that) the function ends there.
Any ideas?
Hi @Akeno, try with this function:
$carbon = Carbon::instance($dateTime);
Carbon::createFromFormat('y-m-d', $carbon);
Hope it helps you.
Is the release_time field a datetime field or a timestamps? If it's a timestamps them I will auto get the current date and time like create_at.
@codeatbusiness thanks but I think need a timestamp like this "0000-00-00 00:00:00"
@fraserk I'm sorry I think I mixed that up. The release_time field is a timestamp, just like 'created_at' and 'updated_at'
Hi @Akeno, the Datetime need to be a real date.
I put the code for example. You need to populate the $datetime variable.
It's needs to be datetime()
I'm sorry, I'm very confused right now^^
why does the Datetime need to be a real date? And what exactly do you mean with 'a real date'?
I changed the column type to "DATETIME" but it still doesn't give me the date I set but the current date :(
Try this
$carbon = Carbon::instance($dateTime);
Carbon::createFromFormat('Y n j h:i:s', $carbon);
@RachidLaasri But what should I set the $datetime variable to? Right now the method receives a $release_time variable that is a Carbon instance
I am not really sure what you trying to do here, do you want the release_time field to be set to the current time?
Or do you get the time from another object or what?
You can try this
Carbon::createFromFormat('Y-m-d H', '2010-10-10 10')->toDateTimeString();
Well:
I have events that have a 'fire_time' where they should run. When you reload your page in the browser it checks for every event with a fire_time that's in the past and runs them. Let's say Event A has a 'fire_time' 5 hours ago. This event will create a "Newslet" (just a piece of information for the user). And the newels should have a release_time of when the Event fired. So release_time = fire_time. Thus, the Event creates a newslet with a 'release_time' 5 hours ago. This should be saved in the database. But when I look into the database the column 'release_time' is not 5 hours ago but it is the current time. That's the problem.
@fraserk Hm, still gives me the current time instead of '2010-10-10 10'
Thats strange..Are you 100% sure the field type is Datetime and not Timestamps
Yes, I'm completely sure
How do you store the date in fire_time field?
That's a Timestamp.
But I converted it to a Carbon instance so this shouldn't matter, or does it? But I'll try changing the column type of fire_time
Edit: didn't change anything :(
You said that you wanna store it as "5 hours ago", so why don't you change the type of release_time to string and do something like :
Carbon::now()->diffForHumans($event->fire_time);
fire_time Must be a Carbon instance.
Can you post the migration file for that table?
No, I meant that colloquial, sorry. I don't know the technical terms for all this stuff (diatomite, timestamp,..). What I meant is:
When it's now:
2015-01-15 21:22:34
'5 hours ago' mean:
2015-01-15 16:22:34
@fraserk ehm, I didn't make one. I created it manually by copying a table that I created with a migration file and changing the columns. The table looks like this: http://i.imgur.com/UZLDqKB.png
Hi @Akeno, for avoiding these errors, the best way to use timestamps is create a migration file and after it you may use the Carbon functionality.
Another problem is related with the new timestamp field that you added, you may need to see the following link:
http://laravel.com/docs/4.2/eloquent#date-mutators
Hope it helps you.
Hm, that could be the answer. So I could recreate the table and use Migration. But how do I declare that a field should be mutated? This way?:
public function getDates()
{
return array('release_time', 'created_at', 'updated_at', 'close_time');
}
And the command for the column should look like this, right?
$table->timestamp('release_time');
Yes.. add the getDates() to the model and the column should be $table->datetime('release_time');
hm, now I have the problem that I cannot migrate via console. It throws an error:
"Access denied for user 'homestead'@'localhost'"
But it should work. On my page it can connect to the database
Maybe you need to change these on .env file
DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
Hi @Akeno, review your config/database.php file or your .env file if you are working with L5.
If you are using homestead your credentials will be homestead and secret as default preferences.
getDates (to save it properly) datetime and timestamp are pretty much the same types, differ only in range Carbon whenever/however you like - it is casts to string thanks to __toString magic method and it returns '2010-10-10 10:10:10' value to the query - it doesn't matter whether you use any of the Eloquent date mutators Carbon object? It is not immutable despite it should be Do you happen to have any listeners for saved? It can't not reach second dd just like that
When you run command in cli use --env=X in order to use provided environment (homestead by default uses config for vagrant db)
My database configuration and the env. file should work fine. Everything works except for migrations.
@JarekTkaczyk Ah okay.
Well, that's the whole method I'm using to create a newslet
public static function create_newslet($object_id, Carbon $release_time, $importance, $content)
{
$newslet = new Newslet();
$newslet->object_id = $object_id;
$newslet->release_time = $release_time;
$newslet->importance = $importance;
$newslet->content = $content;
$newslet->save();
return $newslet;
}
I simply call it from any other method just like this: (notice that I use "Action" as substitution for "Events" because that term is used by laravel. I just call them Events because that's what they are)
public static function run_event_a(Action $action)
{
$call_time = Carbon::parse($action->fire_time);
$planet_id = $action->data->getActionData('planet_id');
$newslet = ActionsController::create_newslet($planet_id, $call_time, 10, "Not enough resources to repair unit");
}
'$action->fire_time' is just a timestamp/datetime from the database saying when the event should be fired. I convert it to Carbon and pass it to the 'create_newslet' method.
That's all.
to 5. => As far as I know, no. I did not set up any listeners at all.
Ok, let's start with this: I'd rather not use controllers for any work of that kind. Use model for this (or repository).
I suppose this is the culprit $call_time = Carbon::parse($action->fire_time);
So first try this simple string:
$newslet->release_time = '2015-01-15 12:00:00';
and check if it is saved as expected.
Oh okay. I though everything that handles and works with data belongs to a controller. I can change that, thanks for telling me that^^. Didn't know it.
Already tried that but it changed nothing, unfortunately.
Show the model and run this:
public static function create_newslet($object_id, Carbon $release_time, $importance, $content)
{
$newslet = new Newslet();
$newslet->object_id = $object_id;
$newslet->release_time = $release_time;
$newslet->importance = $importance;
$newslet->content = $content;
$newslet->save();
dd(\DB::getQueryLog());
return $newslet;
}
and show me the output
Okay, I found the problem. It was at a different place:
First, it gets inserted in the database correctly. But after this, I have a method that mange the 'seen' state of a newslet. If a user has seen it, it can be deleted (after some seconds). And at this point I simply had a typo which (somehow) led to this error. I fixed it and now it's working.
Thank you very much for helping me :) I really appreciate it. I didn't thought the solutions that simple and the cause was my stupidity :D
Please or to participate in this conversation.