Convert a datepicker string into simple date format in php - php

My vuexy datepicker returns the following string:
Tue Dec 01 2020 21:30:00 GMT+0600 (Bangladesh Standard Time)
In my controller (Laravel) I want to convert this into simple date format - yyyy-mm-dd.
I have tried with strtotime() & date() but nothing works. It may need to use DateTime::createFromFormat() but I could not pick the syntax.
How can I convert it into yyyy-mm-dd?

This is working for me:
$date = 'Tue Dec 01 2020 21:30:00 GMT+0600 (Bangladesh Standard Time)';
$date = preg_replace('/\(.*\)$/', '', $date); # Get rid of TZ from the date string
echo date('Y-m-d', strtotime($date));
Here's another sample using the DateTime class
$date = 'Tue Dec 01 2020 21:30:00 GMT+0600 (Bangladesh Standard Time)';
$date = preg_replace('/\(.*\)$/', '', $date);
$dt = new DateTime($date);
echo $dt->format('Y-m-d');
Both Output:
2020-12-01

Hi Friend You Can Use Carbon Package one of the most recomonded package. Please follow the code as below
use Carbon; //use at the begining of class/controller
$date = 'Tue Dec 01 2020 21:30:00 GMT+0600 (Bangladesh Standard Time)';
$formated_date = Carbon::parse($date)->format('Y-m-d');
//output will be 2020-12-01

Related

PHP Change 24 hour datetime using strtotime

I am using strtotime for saving date like strtotime($_POST['end_date']) and this give me format 1441785600 that show very fine in google calendar but when I save string like
Thu Sep 03 2015 14:00:00 (24 hour format)
Data stop displaying in google calendar, I want to convert above sample (24Hours) using strtotime and convert it into something like that 1441785600
You may use something like this which currently doesn't give as you want, but may be useful-
$date = 'Sep 03 2015 14:00:00';
$parsedDate = date_parse_from_format('M d Y H:i:s', $date);
$time_stamp = mktime(
$parsedDate['hour'], $parsedDate['minute'], $parsedDate['second'],
date('n', strtotime($parsedDate['month'])), $parsedDate['day'], $parsedDate['year'],
date('I')
);
You could do:
$dateString = 'Thu Sep 03 2015 14:00:00 (24 hour format)';
echo strtotime(preg_replace('~\w+\s(.+)\s\(.+~','\1',trim($dateString)));

Transform date to valid format for PostgreSQL with PHP

I have a string in this format:
Thu, 26 Feb 2015 11:39:59
And I wonder if its possible to transform it to a valid timestamp format in order to insert it in my PostgreSQL database.
So ideally in the end I would have something like:
2015-03-26 11:39:59
Is there a function in PHP to do something like this?
Use DateTime() to format date string.
$date = 'Thu, 26 Feb 2015 11:39:59';
$date = new DateTime($date);
echo $date->format('Y-m-d H:i:s');
You can also use it as DateTime::createFromFormat
$date = 'Thu, 26 Feb 2015 11:39:59';
$date = DateTime::createFromFormat('D, d M Y H:i:s', $date);
echo $date->format('Y-m-d H:i:s'); //2015-03-26 11:39:59

PHP Date from string

I have a specific date format that i need to convert to a normal looking date.
strtotime() isnt parsing it correctly.
$fulldate = 'Tue Feb 04 2014 09:30:00 GMT-0800 (Pacific Standard Time)';
echo $fulldate.'<br />';
echo strtotime($fulldate).'<br />';
echo date('Y-m-d g:i a', strtotime($fulldate));
Any Ideas?
You can simply use a DateTime object:
<?php
$fulldate = 'Tue Feb 04 2014 09:30:00 GMT-0800 (Pacific Standard Time)';
//remove everything between the brackets
$dateString = preg_replace("/\([^)]+\)/","",$fulldate);
//now, get your date object
$date = new DateTime($dateString);
echo $date->format('Y-m-d H:i:s');
?>
Try the Fiddle here: http://ideone.com/odwtUH
It's not a standard date format because of the last part (between the brackets).
One idea:
$tokens = explode("(", $fulldate);
array_pop($tokens);
$fulldate = trim(join("(", $tokens));
echo strtotime($fulldate);

Converting Date format in PHP

I am getting a date format like Mon Apr 22 2013 12:16:00 GMT+0530 (India Standard Time) form a javascript to my php function.
I want to store that to my database table in "2013-04-22 12:16:00" format.
Can any one help me to convert this date type.
I tried:
date("Y-m-d H:i:s",$startDate);
But it's giving error as
date() expects parameter 2 to be long string given
Use strtotime() and date():
$originalDate = "Mon Apr 22 2013 12:16:00 GMT+0530 (India Standard Time)" ;
$newDate = date("Y-m-d H:i:s", strtotime($originalDate));
(see strtotime and date docs on the PHP site).
or use DateTime:
<?php
$source = "Mon Apr 22 2013 12:16:00 GMT+0530 (India Standard Time)";
$date = new DateTime($source);
echo $date->format("Y-m-d H:i:s"); // 22 2013 12:16:00
echo $date->format("Y-m-d H:i:s"); // 22 2013 12:16:00
?>
DEMO
Try using strtotime to convert the timestamp to unix-format so you can use it in the date() function:
date("Y-m-d H:i:s",strtotime($startDate));
You can also try using the (usually included) DateTime class. In particular, have a look at DateTime::createFromFormat. It may help you get around ambiguities in the date string (strtotime() will sometimes fail or mis-parse a date string). DateTime::createFromFormat allows you to specifically designate the format of the date string so there can be no ambiguity.
firstly you need to save it as Timestamp so,
$startDate = strtotime($javascript_value);
$result = date("Y-m-d H:i:s",$startDate);

Convert a Javascript Date format to desired PHP format

How can we convert Wed Jun 09 2010 which is returns from a javascript function and get in a php script.I need to convert this date format to 2010-06-09.Thanks
<?php
$jsDateTS = strtotime($jsDate);
if ($jsDateTS !== false)
date('Y-m-d', $jsDateTS );
else
// .. date format invalid
Or with DateTime:
$date = new DateTime('Wed Jun 09 2010');
echo $date->format('Y-m-d');
The date format you can input to strtotime(), DateTime and date_create() are explained in the PHP manual. If you need more control over the input format and have PHP5.3, you can use:
$date = DateTime::createFromFormat('D M m Y', 'Wed Jun 09 2010');
echo $date->format('Y-m-d');
Either you can send the javascript date to php via query string:
var mydate = encodeURIComponent('Wed Jun 09 2010');
document.location.href = 'page.php?date=' + mydate;
PHP
echo date('Y-m-d', strtotime(urldecode($_GET['date'])));
Or though a hidden field:
echo date('Y-m-d', strtotime(urldecode($_POST['date'])));

Categories