in my blade there is a DateTime picker which selects in mm/dd/yyyy hh:mm AM/PM
format. and i need to extract this to date = 8/29/2011
and time = 13:00
.
how can I do that?
Asked
Active
Viewed 661 times
1

Dilip Hirapara
- 14,810
- 3
- 27
- 49
-
Does this answer your question? [Converting string to Date and DateTime](https://stackoverflow.com/questions/6238992/converting-string-to-date-and-datetime) – Arun J Dec 31 '19 at 09:36
4 Answers
1
Do as below.
$stringdate = "8/29/2011 1:00 PM";
$timestemp = strtotime($stringdate);
$date = date('Y-m-d', $timestemp);
$time = date('H:i:s',$timestemp);
echo $date;
echo $time;
check example
Edit:- as you want this format YYYY-MM-DD hh:mm
$datetime = date('Y-m-d H:i', strtotime("8/29/2011 1:00 PM"));
echo $datetime; //output "2011-08-29 13:00";

Dilip Hirapara
- 14,810
- 3
- 27
- 49
-
but i want to insert this date and time to a single field in db . so i want to merge it in this format 'YYYY-MM-DD hh:mm . – blonde.addicted.brunet Dec 17 '19 at 06:28
-
ive assigned the date to a variable $input. then i did $datetime = date('Y-m-d H:i', "$input"); but it shows error – blonde.addicted.brunet Dec 17 '19 at 06:42
-
What error are you getting? use `$input` not `"$input"`. so now your code like be `$datetime = date('Y-m-d H:i', $input);` – Dilip Hirapara Dec 17 '19 at 06:48
-
-
message: "A non well formed numeric value encountered", exception: "ErrorException",…} exception: "ErrorException" file: "/var/www/html/bms/app/Http/Controllers/bookSpaceController.php" line: 44 message: "A non well formed numeric value encountered" trace: [{function: "handleError", class: "Illuminate\Foundation\Bootstrap\HandleExceptions", type: "->"},…] – blonde.addicted.brunet Dec 17 '19 at 06:49
-
Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/204385/discussion-between-aljin-abraham-and-dilip-hirapara). – blonde.addicted.brunet Dec 17 '19 at 06:52
-
Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/204465/discussion-between-blonde-addicted-brunet-and-dilip-hirapara). – blonde.addicted.brunet Dec 18 '19 at 06:33
0
Try this way.
@php
$input = '2017-06-27 12:58:20';
$format1 = 'Y-m-d';
$format2 = 'H:i:s';
$date = Carbon\Carbon::parse($input)->format($format1);
$time = Carbon\Carbon::parse($input)->format($format2);
@endphp

Gabrielle-M
- 1,037
- 4
- 17
- 39
0
It is simple just try it
$date="8/29/2011 1:00 PM"; // date that get from view (blade page)
$createTimestamp = new DateTime($date); // create timestamp object from string
$date = $createTimestamp->format('m/d/Y'); // get date
$time = $createTimestamp->format('H:i:s A'); // get time with AM/PM format
return "date: ".$date." time: ".$time; // return your result

bahram
- 92
- 8