I am taking input following Datetime : 12.01.2017 2:25 pm
But while I passed above datetime on php function strtotime then it's doesn't returning exact datetime as 24 hours format.
Here is my scripts:
$sdate="12.01.2017 2:25 pm";
date("d.m.Y H:m", strtotime($sdate))
It's returning always 12-jan-2017 14:01:00
But accurate output should be : 12-jan-2017 14:25:00
Please let me know how can get accurate datetime.
Thanks
hey there is problem on your dateformat string :
it should be H:i instead of H:m on your scripts.
Please correct date and format it accordingly your expectation:
date("d-M-Y H:i", strtotime($sdate));
<?php
$sdate="12.01.2017 2:25 pm";
echo date("d-M-Y H:i", strtotime($sdate));
?>
Output
You should use i instead of m in the date:
like this
echo $sdate="12.01.2017 2:25 pm";
echo date("d.m.Y H:i", strtotime($sdate));
There are already answers that use strtotime(). I would like to show you the DateTime class. I find it very usefull and better looking in general. Not that it matters to most people but still.
// Date string
$dateString = '12.01.2017 11:03 am';
// Create date object from string
$oDate = DateTime::createFromFormat('d.m.Y h:i a', $dateString);
// Output the date
echo $oDate->format('d.m.Y h:i a');
// Or some other format
echo $oDate->format('Y-m-d H:i:s');
Note that date() and DateTime accept the same date parameters. Your string suggests that you will need the following parameters.
d - Day of the month, 2 digits with leading zeros
m - Numeric representation of a month, with leading zeros
Y - A full numeric representation of a year, 4 digits
h - 12-hour format of an hour with leading zeros
i - Minutes with leading zeros
a - Lowercase Ante meridiem and Post meridiem
More information
For more information about date, DateTime, strtotime() and all parameters you can use:
date() - Manual
DateTime - Manual
strtotime() - Manual
date() and DateTime manuals list all possible parameters
Trying to convert a date with the format Jul 27, 2015 5:42:05 PM This is the current way that I'm trying to create the date from the format that I've been provided.
$newDate = new DateTime::createFromFormat('m d, y H:i:s', $game->createDate);
It doesn't like the way that I'm currently doing it. Do I need to try and rework the way that the date comes to me?
This is how I am printing it currently echo date_format($newDate, 'Y-m-d');
createFromFormat is a static method. You don't new it:
$new = DateTime::createFromFormat(...);
it'll do the new business for you internally.
You need to remove the new keyword. In addition, your format string doesn't match the date string you gave. This should work:
$newDate = DateTime::createFromFormat('M d, Y h:i:s a', $game->createDate);
All,
I have a date format in the following output that I get from Tumblr:
2013-02-25 18:00:25 GMT
I'd like to convert this to a date format I want using the date function. I tried the following:
$date = date("F d, Y",$tumblr_posts->date);
echo $date;
When I did this, the output was "January 01, 1970" which is obviously not right. Any ideas on how to fix this?
Thanks!
You need to use the strtotime function like this.
$date = date("F d, Y", strtotime($tumblr_posts->date));
To find how to parse your date use the PHP date function reference.
I have the following string: 2010-04-08T12:46:43+00:00
I want to convert that to:
8th April 2010 # 12:46
Is that easy enough?
Take a look at strtotime to create a UNIX timestamp from your time string, and then use date($format, $UNIXtimestamp); to create a normal date again:
$Timestamp = strtotime("2010-04-08T12:46:43+00:00");
echo date("your time format", $Timestamp);
You can look up the specific characters for the time format from PHP.net
Here you go.
EDIT : Exactly as you needed
Code:
$time_value = strtotime("2010-04-08T12:46:43+00:00");
$date_in_your_format = date( 'jS F Y # g:i', $time_value);
echo $date_in_your_format;
yep. use strtotime() + date()
Try taking a look at strtotime() and date().
I am pulling the dates of various posts from a database. The dates are in the following format:
2009-08-12
Numeric Year - Numeric Month - Numeric Day
How can I reformat these dates to something more user friendly like:
August 12, 2009
Numeric Month Numeric Date, Numeric Year
Assuming that the date gotten from the mysql database is stored in a variable called:
$date = $row['date_selected'];
Unlike the strtotime based examples, this allows you to ensure the month and day are interpreted in the correct order regardless of locale settings specified on the server.
$date = DateTime::createFromFormat('Y-m-d', '2009-08-12');
$output = $date->format('F j, Y');
date("F d, Y", strtotime($input))
$new_format = date("Your Date String", strtotime($date));
See:
- http://php.net/strtotime
- http://php.net/date
Basically, if strtotime() can read it correctly, you can reformat it anyway you please.
In this case, Year - Month - Day is a properly recognized strtotime() format, this might not be the case for other formats.
You might consider doing your date formatting in MySQL with your select statement:
DATE_FORMAT(date,'%M %e, %Y') as date_selected
http://www.w3schools.com/sql/func_date_format.asp
<?php
echo date('F j, Y', strtotime($date));
You might want to look at the php function strtotime:
http://php.net/manual/en/function.strtotime.php
It'll parse a large number of date representations to a Unix timestamp.
Then use the date function.
Using strtodate or explode to split the date into its different components, you can then use the date function with the appropriate format string:http://php.net/manual/en/function.date.php
$date = "2009-08-12";
list($year,$month,$day) = explode("-",$date);
$formattedDate = date("F d, Y", mktime(0,0,0,$month,$day,$year));
Outputs: "August 12, 2009"
<?php
//Date Formatter
/*
date: date you want to convert
format: its current format ie m-d-Y, m/d/Y, Y-m-d, Y/m/d
delimS: Current delimiter ie - or / or .
delimF: The delimiter you want for the result
NOTE: this will only convert m-d-Y to Y-m-d and back
*/
function dtform($date,$format,$delimS,$delimF){
$dateFinal = '';
if($format == 'm'.$delimS.'d'.$delimS.'Y'){
$dateFinal_exp = explode($delimS,$date);
$dateFinal = $dateFinal_exp[2].$delimF.$dateFinal_exp[0].$delimF.$dateFinal_exp[1];
}else if($format == 'Y'.$delimS.'m'.$delimS.'d'){
$dateFinal_exp = explode($delimS,$date);
$dateFinal = $dateFinal_exp[1].$delimF.$dateFinal_exp[2].$delimF.$dateFinal_exp[0];
}
return $dateFinal;
}
?>
Use it like this:
// February 1, 2005
print date ("F j, Y", mktime (0,0,0,14,1,2004));