Laravel Carbon cast datetime to set seconds to 00 - php

If I have variables like this:
$arivalTime1 = '2020-06-05 15:52:27'
$arivalTime2 = '2020-06-05 15:52:55'
how can I convert these variables using Carbon to have the seconds displayed as 00:
$converted1 = 2020-06-05 15:52:00
$converted2 = 2020-06-05 15:52:00

there is many ways to do it:
1- like Vladimir verleg 's answer:
$arivalTime1 = '2020-06-05 15:52:27'
Carbon::parse($arivalTime1)->format('Y-m-d H:i:00');
2- using startOfMinute() method:
Carbon::parse($time)->startOfMinute()->toDateTimeString();
3- using create method witch gave you more control:
$value=Carbon::parse($time);
$wantedValue=Carbon::create($value->year,$value->month,$value->day,$value->hour,$value->minute,0);

Thank you #OMR for your solution given in the comments:
Carbon::parse($time)->startOfMinute()->toDateTimeString()

Parse your date-string using Carbon and format it, setting the seconds part to 00:
$arivalTime1 = '2020-06-05 15:52:27';
echo Carbon::parse($arivalTime1)->format('Y-m-d H:i:00');

Related

How to get timestamp like this : 0000-00-00 00:00:00.000000 in php [duplicate]

I echo this :
php> echo date("Y-m-d\TH:i:s");
2011-05-27T11:21:23
How can do with date function to get this date format:
2011-01-12T14:41:35.7042252+01:00 (for example)
35.7042252 => seconds.decimal-fraction-of-second
I have tried:
php> function getTimestamp()
... {
... return date("Y-m-d\TH:i:s") . substr((string)microtime(), 1, 8);
... }
php> echo getTimestamp();
2011-05-27T15:34:35.6688370 // missing +01:00 how can I do?
date('Y-m-d\TH:i:s.uP')
u for microseconds was added in PHP 5.2.2. For earlier or (still) broken versions (see comments):
date('Y-m-d\TH:i:s') . substr(microtime(), 1, 8) . date('P')
Or, to avoid two calls to date:
date(sprintf('Y-m-d\TH:i:s%sP', substr(microtime(), 1, 8)))
Best performance:
substr_replace(date('c'), substr(microtime(), 1, 8), 19, 0);
By doing separate [date] calls you have a small chance of two time stamps being out of order: eg call date at 1:29:22.999999 and mircotime at 1:29:23.000001. On my server consecutive calls are about 10 us apart.
Source
Try this instead:
list($usec, $sec) = explode(" ", microtime());
echo date("Y-m-d\TH:i:s", $sec) . substr($usec, 1, 8) . date("P", $sec);
E.g.:
2015-07-19T16:59:16.0113674-07:00
As a solution for 2020 with the DateTime class, which was added in PHP 5.2, you can do a simple one liner to get the wanted format.
For example:
echo (new DateTime())->format('Y-m-d\TH:i:s.uP');
// 2020-04-23T09:18:25.311075+02:00
The DateTimeInterface, which is implemented by the DateTime class, brings its own constants for widely used date formats. If you know the format, you can use a constant for that.
echo var_dump($datetime->format(DateTime::RFC3339_EXTENDED));
// 2020-04-23T09:18:25.311+02:00
As object orientated programming is widely spread in the PHP world, this could be a possible solution, too.
If parsing the string returned by microtime makes you vomit in your mouth, and you don't want multiple distinct timestamps munged together into your output, you can do this:
$unow = microtime(true);
sprintf("%s.%06d%s", date("Y-m-d\TH:i:s", $unow), ($unow - floor($unow))*1e6, date("P", $unow));

Highchart add three zeros in the loop

I have use library chart from this page link. Unfortunately, the data I download is not compatible, for example:
I get from JSON time:
time: 1346803200
this time is not displayed on the chart. I must add three zeros at the end (look like this: 1346803200000), then the chart displays correctly. So I have code:
for ($i=0; $i < count($chart['Data']) ; $i++) {
$time = $chart['Data'][$i]['time'];
}
I need add to variable $time numeric 000 (three zeros at the end). I can not add it this way:
$time = $chart['Data'][$i]['time']."000";
because variable $time change from int to string. I must have $time in integer type. Is there any way to add three zeros without changing the variable type inside the loop ?
Not sure why you are doing this or if there is a better way, but if type conversion is the only thing that worries you, you can explicitly cast it to int:
$time = (int)($chart['Data'][$i]['time']."000");
Also, not sure if this is your desired behavior, but just note that your $time variable will get overwritten with every iteration of the for loop.
And one more thing, you can achieve your desired output without the explicit conversion by just multiplying your result with 1000, like so:
$time = $chart['Data'][$i]['time'] * 1000;
This should be a better solution than concatenation when you are working with ints
Seriously?
$time = $chart['Data'][$i]['time'] * 1000;
You con multiply for 1000
$time = $chart['Data'][$i]['time']*1000;

how to extract only time from DATETIME column

I am trying to extract the time from each field for now what i get is:
if i use the following code:
$row["real_stime"] = $row["start_time"];
$row["real_etime"] = $row["end_time"];
i get this output:
"real_stime":"2015-11-18 07:18:00","real_etime":"2015-11-18 20:18:00"
and if i use this code:
$row["formated_start_time"] = date("H:i",$row["start_time"]);
$row["formated_end_time"] = date("H:i",$row["end_time"]);
i get this output:
"formated_start_time":"00:33","formated_end_time":"00:33"
the output that i need is:
07:18,20:18
Use H for 24 hour format and h for 12 hour format
$row["formated_start_time"] = date("H:i",strtotime$row["start_time"]));
$row["formated_end_time"] = date("H:i",strtotime$row["end_time"]));
See this link for more information on formats
See Demo Here
use date('H:i:s')
this should be
date("G:i:s",$row["start_time"])
^
change to this
date("H:i:s",$row["start_time"]);
So final well-form code is
$row["formated_start_time"] = date("H:i:s",$row["start_time"]);
$row["formated_end_time"] = date("H:i:s",$row["end_time"]);

PHP format date

How can I force the date format to output:
12/12/2012, 1/10/2012, 1/5/2012
instead of
12/12/2012, 01/10/2012, 01/05/2012?
My code is the following:
$adatefrom = date_create($_POST['datefrom']);
$adateto = date_create($_POST['adateto']);
$adatefrom = date_format($adatefrom, 'd/m/Y');
$adateto = date_format($adateto, 'd/m/Y');
Please do note that I have to format the date AFTER posting it.
Have a look at the PHP built in date function here
You will find that your solution is as simple as this:
date('j/n/Y',strtotime($_POST['datefrom']));
The key things to note are the characters used in the first parameter.
j represents the day without leading zeros
n represents the month without leading zeros
There are many other options you have, just have a read through the documentation.
Please note that a simple search of 'PHP date' on Google would have found this solution for you
$adatefrom = date_create($_POST['datefrom']);
$adateto = date_create($_POST['adateto']);
$adatefrom = date_format($adatefrom, 'j/n/Y');
$adateto = date_format($adateto, 'j/n/Y');
you are welcome! ;)

how to delete firstdigit 0 from date and month

Code:
$today_mem = date("d.m");
echo $today_mem; // -> 15.02
I need to transform dates like 15.02 into 15.2, also need to transform for ex. 07.02 into 7.2 and so on every day.
So the question is: how to delete firstdigit 0 from date and month.
Any short solutions?
You'll want to use:
$today_mem = date("j.n");
echo $today_mem; // -> 15.2
To remove the leading zeros. See more format modifiers at: php.net/date
Use j instead of d and n instead of m:
$today_mem = date("j.n");
Reference at the PHP doc site.
use
$today_mem = date("j")

Categories