Convert PHP Date to Time - php

How can I covert this PHP date string:
Thu 19th Aug 2010 # 7:52PM
to this:
1282247549
Which is done by:
gmdate("D jS M Y # g:iA", $row['project_deadline'])
The timestamp is from the database with the stored time() function

You can parse that string into a UNIX timestamp with the strtotime function:
$str = 'Thu 19th Aug 2010 # 7:52PM';
$str = str_replace('#', '', $str);
$timestamp = strtotime($str);

If you have unstandard format, then you should use DateTime::createFromFormat :
$str = 'Thu 19th Aug 2010 # 7:52PM';
$dt = DateTime::createFromFormat('!D jS M Y # g:iA', $str);
echo $dt->getTimestamp();

Related

PHP change date format from long date with time to basic date

I currently have the date in the format: Day, day month year 24hr with seconds and timezone. I want to get this into Y-m-d.
Current Date:
Fri, 23 Mar 2018 20:28:34 0000
Final Date:
2018-03-23
I am using PHP to do this. The date is hard coded so I am unable to call the date() function at all.
Another alternative:
$str = "Fri, 23 Mar 2018 20:28:34 0000";
$str = trim( preg_replace( "/[0-9\+]*$/", "", trim($str) ) );
echo date( "Y-m-d", strtotime($str) );
Output:
2018-03-23
You can try using this code
echo date( "Y-m-d", strtotime(preg_replace( "/[0-9]+$/", "", "Fri, 23 Mar 2018 20:28:34 0000")) );
This will output the same answer you required
You can use createFromFormat() to transform your date:
$str = "Fri, 23 Mar 2018 20:28:34 0000";
$str = preg_replace('~\s(\d{4})$~', '+$1', $str); // change '0000' to '+0000'
$d1 = DateTime::createFromFormat("D, d M Y H:i:s O", $str) ;
echo $d1->format("Y-m-d") ;
// or
// echo date("Y-m-d", strtotime($str)) ;
Outputs:
2018-03-23
Or, as pointed out by #Andreas, you could use substr() to remove timezone:
$str = "Fri, 23 Mar 2018 20:28:34 0000";
$str = substr($str, 0,-5) ;
echo date("Y-m-d", strtotime($str)) ;
If the date is hardcoded as you say, that means it will always have a three lettered day, a comma then the date, then time and timezone.
The length of the string should be the same always.
This can be used to an advantage.
$str = "Fri, 23 Mar 2018 20:28:34 0000";
Echo date("Y-m-d", strtotime(substr($str,5,11)));
Here I use only the 23 Mar 2018 in the strtotime and date function.
No need for regex or trimming or adding + or - to timezones.

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

Get the day from a very simple date

Is there a simple way to get the day from a very simple date string?
For example:
14 mar 2012
gets converted to:
Wed, 14 mar 2012
You should familiarize yourself with DateTime:
$str = "14 mar 2012";
$date = DateTime::createFromFormat("j M Y", $str);
$str = $date->format("D, j M Y");
$dateUnformated = '14 mar 2012';
$dateFormated = date("D, j M Y", strtotime($dateUnformated));

Timestamp to normal date format and reverse conversion using PHP

I have a timestamp as 2011-08-27 18:29:31. I want to convert it to 27 Aug 2011 06.29.31 PM. Also, I want to convert this format reverse back to the previous timestamp format.
How van I do this using PHP?
$converted = date('d M Y h.i.s A', strtotime('2011-08-27 18:29:31'));
$reversed = date('Y-m-d H.i.s', strtotime($converted));
Don't use date()! It`s too old function. In PHP v. 5.2 and more you should use date_format or DateTime::format object.
you can use the date_format() function
//Convert to format: 27 Aug 2011 06.29.31 PM
$converted_date = date_format('d M Y h.i.s A',strtotime($orig_date));
//Convert to format 2011-08-27 18:29:31
$converted_date = date_format('Y-m-d H:i:s',strtotime($orig_date));
Have you looked at the date functions in PHP, especially strftime (for formatting a timestamp) and strtotime
To convert from 2011-08-27 18:29:31 to 27 Aug 2011 06.29.31 PM:
echo date('d M Y, H.i.s A', strtotime('2011-08-27 18:29:31'));
To do the reverse:
echo date('Y-m-d H:i:s',strtotime('27 Aug 2011 06.29.31 PM'));
If that doesn't work, you may have to try:
$date = date_create_from_format('d M Y, H.i.s A', '27 Aug 2011 06.29.31 PM');
echo date_format("Y-m-d H:i:s",$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