PHP Date from string - php

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);

Related

Convert a datepicker string into simple date format in 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

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

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'])));

How to convert yyyy-MM-dd HH:mm:ss to "15th Apr 2010" using PHP

I have the following date format: 2010-04-15 23:59:59
How would I go about converting this into: 15th Apr 2010
Thanks
echo date("jS M Y",strtotime("2010-04-15 23:59:59"));
In addition to using date and strtotime, you can do
$dateTime = new DateTime('2010-04-15 23:59:59');
echo $dateTime->format('jS M Y'); // 15th Apr 2010
or
$dateTime = date_create('2010-04-15 23:59:59');
echo date_format($dateTime, 'jS M Y'); // 15th Apr 2010
$date = date("dS M Y",strtotime("2010-04-15 23:59:59"));
print $date;

Date formatting in PHP

I've a date formatted like "Tue Jan 05 11:08:27 +0000 2010" and I want to convert it's format to "yyyy-mm-dd 00:00" in PHP.
How can I do that?
convert it to a PHP date object with strtotime() then output it with date()
EDIT
Some more detail; try:
$time = strtotime('Tue Jan 05 11:08:27 +0000 2010');
echo date("Y-m-d h:i", $time);
Y = 4 digit year
m = 2 digit month (with leading 0)
d = 2 digit month (with leading 0)
h = 12 hour time (leading 0)
i = minutes (with leading 0)
http://php.net/manual/en/function.date.php
for all the formatting options
$time_string = 'Tue Jan 05 11:08:27 +0000 2010';
$formated_time = date('Y-m-d h:i', strtotime($time_string));
echo $formated_time;
strtotime + date
Agree with Erik, if you want to do it in one line.
Solution
$date = date('Y-m-d H:i:s', strtotime('Tue Jan 05 11:08:27 +0000 2010'));

Categories