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');
Related
I want to get the timestamp in 15 minutes from now.
I don't want to use strtotime, I want to use DateTime and DateInterval.
I'm doing:
$now = new DateTime('now');
$in_15_m = $now->add(new DateInterval('PT15M'));
echo 'Now:' . $now->format('Y-m-d\TH:i:s\Z');
echo 'In 15 min': . $in_15_m->format('Y-m-d\TH:i:s\Z');
But both printed lines contain the same date.
How can I achieve this?
The arguments for DateInterval aren't really clear in the docs, though I think I am using it the correct way.
Thanks.
The DateTime::add method modifies the DateTime object in place. It also returns the modified object for use in method chaining, but the original object is still modified.
You can use DateTimeImmutable instead:
$now = new DateTimeImmutable('now');
$in_15_m = $now->add(new DateInterval('PT15M'));
echo 'Now:' . $now->format('Y-m-d\TH:i:s\Z');
echo "\n+15m:" . $in_15_m->format('Y-m-d\TH:i:s\Z');
Or, alternatively, use two objects:
$now = new DateTime('now');
$in_15_m = (new DateTime('now'))->add(new DateInterval('PT15M'));
echo 'Now:' . $now->format('Y-m-d\TH:i:s\Z');
echo "\n+15m:" . $in_15_m->format('Y-m-d\TH:i:s\Z');
What is the "cleanest" way to add a date and a time string in PHP?
Albeit having read that DateTime::add expects a DateInterval, I tried
$date = new \DateTime('17.03.2016');
$time = new \DateTime('20:20');
$result = $date->add($time);
Which was no good and returned nothing to $result.
To make a DateInterval from '20:20', I only found very complex solutions...
Maybe I should use timestamps?
$date = strtotime($datestring);
$timeObj = new \DateTime($timestring);
// quirk to only get time in seconds from string date
$time = $timeObj->format('H') * 3600 + $timeObj->format('i') * 60 + $timeObj->format('s');
$datetime = $date+$time;
$result = new \DateTime;
$result->setTimestamp($datetime);
In my case, this returns the desired result, with the correct timezone offset. But what do you think, is this robust? Is there a better way?
If you want to add 20 hours and 20 minutes to a DateTime:
$date = new \DateTime('17.03.2016');
$date->add($new \DateInterval('PT20H20M'));
You do not need to get the result of add(), calling add() on a DateTime object will change it. The return value of add() is the DateTime object itself so you can chain methods.
See DateInterval::__construct to see how to set the intervals.
DateTime (and DateTimeImmutable) has a modify method which you could leverage to modify the time by adding 20 hours and 20 minutes.
Updated
I've included examples for both DateTime and DateTimeImmutable as per the comment made, you don't need to assign the outcome of modify to a variable because it mutates the original object. Whereas DateTimeImmutable creates a new instance and doesn't mutate the original object.
DateTime
<?php
$start = new DateTimeImmutable('2018-10-23 00:00:00');
echo $start->modify('+20 hours +20 minutes')->format('Y-m-d H:i:s');
// 2018-10-23 20:20:00
Using DateTime: https://3v4l.org/6eon8
DateTimeImmutable
<?php
$start = new DateTimeImmutable('2018-10-23 00:00:00');
$datetime = $start->modify('+20 hours +20 minutes');
var_dump($start->format('Y-m-d H:i:s'));
var_dump($datetime->format('Y-m-d H:i:s'));
Output
string(19) "2018-10-23 00:00:00"
string(19) "2018-10-23 20:20:00"
Using DateTimeImmutable: https://3v4l.org/oRehh
I' am trying to convert the date of next 7 days into timestamp so that I can compare against my date timestamp in database to get some results.
This function is used to get the next 7 days from today
$next_date = date("d/m/Y", strtotime("7 day"))
Output
30/04/2014
Now I' am again running strtotime() on $next_date variable who holds the next 7days and converting to timestamp.
echo strtotime($next_date);
This is not working. I followed this stackoverflow answer and few others.
As an alternative suggestion you could look at PHP's internal DateTime() and DateInterval() classes. It makes it a bit easier to convert between formats and do date/time addition and subtraction imho. DateInterval requires at least PHP version 5.3.
An example:
// create a current DateTime
$currDate = new DateTime();
// copy the current DateTime and
// add an interval of 7 days
$nextDate = clone $currDate;
$nextDate->add(new DateInterval('P7D'));
// both objects are easily converted to timestamps
echo $currDate->getTimestamp(); // e.g: 1398296728
echo $nextDate->getTimestamp(); // e.g: 1398901528
// and both can be easily formatted in other formats
echo $currDate->format('d/m/Y'); // e.g: 24/04/2014
echo $nextDate->format('d/m/Y'); // e.g: 01/05/2014
EDIT
For completeness, here's another example of how you can add seven days to a DateTime object:
$now = new DateTimeImmutable();
$then = $now->modify('+7 days');
var_dump($now->format('Y-m-d'), $then->format('Y-m-d'));
Yields:
string(10) "2016-05-24"
string(10) "2016-05-31"
You can also use DateTime - the difference in this use case is that DateTime::modify() will modify the instance $now where DateTimeImmutable::modify() will return a new DateTimeImmutable object - so if you need to create a new object whilst retaining the old one, it's probably the most succinct approach.
Hope this helps :)
http://www.php.net/manual/en/datetime.construct.php
http://www.php.net/manual/en/dateinterval.construct.php
Just store the value from strtotime first?
$timestamp_in_7_days = strtotime('7 day');
$next_date = date('d/m/Y', $timestamp_in_7_days);
There is no need to throw the time back and forth between unix timestamp and date-format.
How do I add the difference between two DateTime objects to another DateTime object? I tried some code similar to the one below, but it didn't work.
$first_time=new DateTime('01/01/2000 00:00:00');
$second_time=new DateTime('01/01/2000 00:00:50');
$diff=$first_time->diff($second_time);
$time=new DateTime('01/01/2012 12:00:00');
$time->modify('+'.$diff->format('%s').' seconds');
echo $time;
//Should echo: "01/01/2012 12:00:50"
Can somebody help me out?
format() does not calculate the absolut number of seconds of the Interval, it just give you the values of the intern attributes. Since you want to add, why not simply use add()? diff() returns a DateInterval object, and this is what add() needs.
$first_time=new DateTime('01/01/2000 00:00:00');
$second_time=new DateTime('01/01/2000 00:00:50');
$diff=$first_time->diff($second_time);
$time=new DateTime('01/01/2012 12:00:00');
$time->add($diff);
echo $time;
I am tring to get a set time timestamp for e.g
if I input the follow time 09:00 i want to see the timestamp for today
function GetTimestamp($time){
date();
}
return GetTimestamp("09:00")
can someone help me please or lead me down the right path
You just have to use the strtotime() function, passing it your time :
$ts = strtotime('09:00');
var_dump($ts);
And you'll get :
int 1300435200
Note : if you don't specify the date, strtotime will use today.
You could also use the DateTime class :
$dt = new DateTime('09:00');
$ts = $dt->format('U');
var_dump($ts);
Use strtotime:
$timestamp = strtotime('09:00');