I need a function that takes any date/time and re formats it to a different format.
e.g.
$date = "31/12/2010 15:00:00" => has a format of d/m/Y H:i:s
so if i need a different format like
$returning_format = "m/d/Y H:i:s" => for America date/time
$newdate = transoform_date($current_format,$returning_format,$date);
the above line should return 12/31/2010 15:00:00
NOTE: I don't need it for this format only coz i can explode it and re-arrange it, so it can accept any format and return the desired format date.
For PHP 5.3+
$newdate = DateTime::createFromFormat($current_format,$date)->format($returning_fomrat);
strtotime — Parse about any English textual datetime description into a Unix timestamp
function transform_date($return_format, $date)
{
return date($return_format, strtotime($date));
}
Related
I need to conver timestamp to 2016-07-12 format. This is what I tried.
$selectedDate=date('m/d/Y H:i:s', '1465430400000');
I got 08/23/48407 00:00:00 I need to conver it to 2016-07-12 format.
Please Note: Here the format m/d/Y H:i:s isn't the matter. I'm getting wrong date is the problem
Any suggetion would be appricieated.
It looks like your timestamp is 1000x what date() expects, so try first dividing it by 1000 (and then, of course, use the right date format):
$selectedDate = date('Y-m-d', 1465430400000/1000);
You can convert Date in Any format:
<?php $date1 = strtotime($old_date);
echo $date = date("y-M-d", $date1); ?>
Complete list of format options
I am having problems with dates in php- sometimes the date gets to us in d/m/y and other times its d/m/Y. I want to convert all dates to d/m/Y.
Working with my current dataset, how would I get 24/06/2015 from 24/06/15 using php?
So far I have tried :
$original_date = '24/06/15';
$new_date = date('d/m/Y', strtotime($original_date));
This brings back 01/01/1970
This is probably the most robust method:
$string = '24/06/15';
$date = DateTime::createFromFormat('d/m/y', $string) ?: DateTime::createFromFormat('d/m/Y', $string);
echo $date->format('d/m/Y');
createFromFormat returns false if you try to parse 24/06/2014 using the d/m/y format, so in that case you just retry with d/m/Y. You then get a DateTime object which you can format and output any way you like.
use the lowercase 'y'. See the PHP date manual.
$new_date = date('d/m/y', strtotime($original_date));
y = A two digit representation of a year
The problem is that the strtotime doesn't recognise the UK date format, so convert the format first then format the date.
Try this:
$original_date = "24/06/15";
list($date,$month,$year) = sscanf($original_date, "%d/%d/%d");
$date_convert = $year."-".$month."-".$date;
$new_date = date("d/m/Y", strtotime($date_convert));
echo $new_date;
Its wrong format of date you are using for strtotime.
Have a look at Date Formats
The correct code should have
$original_date = '15/06/24'; // Notice : its mm/dd/yy here
$new_date = date('d/m/Y', strtotime($original_date));
Given a date as a string like 'October 12, 2010'?
Should there be some Date "type" I should be converting to. Or is it just a matter of converting it to another string formatted as '2010-10-12' If so, what is the simplest way to convert to the yyyy-mm-dd format given my starting format?
Yes, DATE fields in MySQL just need to be a string in the correct format. To convert your date to the right format, use date combined with strtotime.
$date = 'October 12, 2010';
$sqlDate = date('Y-m-d', strtotime($date)); // 2010-10-12
Note: 'Y-m-d' is for a DATE field, if you're using DATETIME, use 'Y-m-d H:i:s' instead.
$date = new DateTime('October 12, 2010');
$sqldate = $date->format('Y-m-d');
DateTime is PHP 5 >= 5.2.0
You could also use strtotime() however that tends to break if you want to use a date in the far future (on 32b systems).
When I had UNIX timestamps, I'd write:
strftime("%A", $date)
But now I have datestamps, like "2011-08-02"
How can I make it output the weekday name, e.g "Sunday"?
You can convert the date stamp to a timestamp using the function strtotime.
Once you have the timestamp, you can just use the function date to show the date in the desired format.
First use the strtotime function to convert the '2011-08-02' to a UNIX timestamp, and then proceed as you usually would
For example, the following are equivalent:
$date = 1312243200; // A unix timestamp
$date = strtotime('2011-08-02'); // The date that it represents
You can then do whatever you would usually do with the result
The strtotime() function is fairly forgiving in what date formats it accepts and even accepts values such as '8pm tomorrow' or 'last Monday' - See http://www.php.net/strtotime
Use date('l'); Add more properties like so: date('l d-m-Y');.
More info here
(2nd August 2011 was a Tuesday, not a Sunday.)
<?php
echo strftime("%A", strtotime("2011-08-02"));
// Output: "Tuesday"
?>
Live demo
strtotime documentation
date('l',strtotime('2011-08-02'));
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));