This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I receive a date from an array in the below format:
2014-10-22 07:24:57 EDT
I want to convert this to echo only the month and day like this: Oct 22
My code below works fine when I'm changing from the 15-Feb-2009 format.
<?php
$date = DateTime::createFromFormat('D-M-Y', '15-Feb-2009');
$nice_date = $date->format('M j');
echo $nice_date;
?>
Here's what I have that doesn't work. Probably not even close.
$array_date = '2014-10-22 07:24:57 EDT';
$date = DateTime::createFromFormat('YY-MM-DD HH:II:SS EDT', $array_date);
$nice_date = $date->format('M j');
echo $nice_date;
I'm at a loss right now. Any ideas?
If you want to explicitly provide the format, you must provide the correct format.
$array_date = '2014-10-22 07:24:57 EDT';
$date = DateTime::createFromFormat('Y-m-d H:i:s e', $array_date);
$nice_date = $date->format('M j');
echo $nice_date;
$str = "2014-10-22 07:24:57 EDT";
echo date("M d", strtotime($str)); // Oct 22
Try following ( i used date and strtotime ) :
$originalDate = "2014-10-22 07:24:57 EDT";
$newDate = date("M j", strtotime($originalDate));
echo $newDate;
$array_date = '2014-10-22 07:24:57 EDT';
$date = DateTime::createFromFormat('YY-MM-DD HH:II:SS EDT', $array_date);
$nice_date = date('M d',$date);
echo $nice_date;
Related
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I want to convert date into d-M-y format and it seems that i am doing something wrong. Kindly help me to correct it.
<?php
$date = '30/04/2017';
echo date('d-M-y', strtotime($date));
?>
My Output:
31-Dec-69
By I want output as 30-Apr-17
Use DateTime objects when you're working with dates and times. You can use DateTime::createFromFormat() to parse the date string and then the DateTime::format() to format it the way you want:
<?php
$str = '30/04/2017';
$date = DateTime::createFromFormat('d/m/Y', $str);
echo $date->format('d-M-Y');
?>
Working Demo
Use DateTime::createFromFormat()
$date = DateTime::createFromFormat('d/m/Y', '30/04/2017');
echo $date->format('d-M-Y');
Try this:
<?php
$date = '30/04/2017';
$newDate = str_replace('/', '-', $date);
echo date('d-M-y', strtotime($newDate));
?>
// Output: 30-Apr-17
Working Example
<?php
$date = '30-04-2017';
echo date('d-M-y', strtotime($date));
?>
or use
<?php
$date = '25/05/2010';
$date = str_replace('/', '-', $date);
echo date('d-M-y', strtotime($date));
?>
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I get the date in the variable date as
$date = "<?php echo $projects->dob;?>" ; //where let dob=2013-01-12
I want to convert this into Jan 2013 format. How to convert in php?
do:
echo date('M Y', strtotime($your_date));
see: Date Formats
$dob = '2013-01-12';
echo date('M Y', strtotime($dob));
$date = date('M Y', strtotime($projects->dob));
Example:
https://ideone.com/tdJl8O
$timestamp = strtotime("2013-01-12");
$formatted = date("M Y", $timestamp);
Demo: http://codepad.org/Lw2eEq6Q
echo $date = date("M Y",strtotime($projects->dob)) ;
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I am importing data from and excel sheet and the dates are formatted like so:
May 5, 2014
I need to convert this to a proper PHP date format like:
Y-m-d
This isn't working:
$new_date = date('Y-m-d', strtotime('May 5, 2014'));
echo $new_date;
Results
1969-12-31
I was thinking that i could do something like this:
$old_date = 'May 5, 2014';
$old_date = str_replace(',', '', $old_date);
$old_date_parts = explode(' ',$old_date);
// Convert month name to number
$may = '5';
// Build new date
$new_date = $old_date_parts[2].'-'.$may.'-'.$old_date_parts[1];
But there HAS to be another way!
Please help. Thank you in advance.
EDIT
Seems that the PHPExcel that i am using doesn't have the function to convert a serial date to a unix time so i used this.
UNIX_DATE = (EXCEL_DATE - 25569) * 86400
$date = date_create_from_format('M j, Y', 'May 5, 2014');
echo date_format($date, 'Y-m-d');
Or OOP Style
$date = DateTime::createFromFormat('M j, Y', 'May 5, 2014');
echo $date->format('Y-m-d');
$date_string = "1 October, 2013";
$date_time = strtotime($date_string); //evaluates to "1412208780"
$date_string_again = date("m/d/y", $date_time); //evaluates to "10/01/14" .. should be "10/01/13"
Why is my code giving me the wrong year? How do I fix it?
Make use of DateTime::createFromFormat
<?php
$dt = '1 October, 2013';
$date = DateTime::createFromFormat('j F, Y', $dt);
echo $date->format('m/d/Y'); //"prints" 10/01/2013
Indeed, the comma is throwing it off. Remove it!
$date_string = "1 October, 2013";
$date_string = str_replace(',','',$date_string);
$date_time = strtotime($date_string);
$date_string_again = date("m/d/y", $date_time);
Your string is not a valid format that strtotime() recognizes, see the manual for accepted formats.
If the format is fixed like this, you could use DateTime::createFromFormat to convert your date to a DateTime object.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Format mysql datetime with php
How to convert the following:
2010-12-07 12:00:00
into:
December, 7th, 2010
echo date("F, jS, Y", strtotime("2010-12-07 12:00:00"));
http://php.net/manual/en/function.date.php
Check out the date function.
$myDate = strtotime('2010-12-07 12:00:00');
$formattedDate = date('F, jS, Y', $myDate)
Use date_format
http://www.php.net/manual/en/function.date-format.php
$date = date_create('2010-12-07 12:00:00');
echo date_format($date, 'F, jS, Y');
echo date('F, jS, Y',strtotime('2010-12-07 12:00:00'));
$d = strtotime('2010-12-07 12:00:00');
echo date('F, jS, Y', $d);