I've some data which formatted like: 04.09.1953
I want to convert this format to: 1953-09-04
Is there any way or php function to do this?
just use strtotime() to get a timestamp and then date() to convert that timestamp to the format you need:
$timestamp = strtotime("04.09.1953");
echo date("Y-m-d", $timestamp);
EDIT:
If you're having some "exotic" format as input, you might need to use explode(), list() and mktime() to build the timestamp on your own:
list($y,$m,$d) = explode(".","04.09.1953");
$timestamp = mktime(0,0,0,$m,$d,$y);
echo date("Y-m-d", $timestamp);
Have you tried strtotime() ? It might work, else you'll need to do manual conversion using substrings or explodes.
http://php.net/strtotime
http://php.net/substring
http://php.net/explode
If all you are interested in is converting from one "string" format to another you can use a regEx:
$DMY = '04.09.1953';
$YMD = preg_replace('/(\d\d).(\d\d).(\d{4,4})/', '$3-$2-$1', $DMY);
http://www.handyphp.com/index.php/PHP-Resources/Handy-PHP-Functions/reformat_date.html
function reformat_date($date, $format){
$output = date($format, strtotime($date));
return $output;
}
Related
My API outputs the DateTime in the following format:
2018-06-17T09:07:00Z
How do I display this in a more meaningful way, say, 17/06/2018.
I looked at the Manual: http://php.net/manual/en/datetime.formats.date.php however still wasn't able to find a way to achieve this.
$eventStart = "2018-06-17T09:07:00Z";
You can format it like the below code in PHP:
echo date('d/m/Y', strtotime('2018-06-17T09:07:00Z'));
Use Below code.
date('d/m/Y', strtotime('2018-06-17T09:07:00Z'));
Use Date Format :
$inputDate = "2018-06-17T09:07:00Z";
echo date('d/m/Y', strtotime($inputDate));
Convert the string in time and then format the date which you want
Date-Format option
$date = '2018-06-17T09:07:00Z';
echo date('d/m/Y', strtotime($date));
Format the date, but first convert the string to time.
echo date('d/m/Y', strtotime($inputDate));
I have much data with several timestamps and I just recognized that some are in "dd.mm.YYYY" which works very well with date("Y-m-d", strtotime($input)); but some are in "dd.mm.YY" and this does not work anymore - it always returns the current date.
My problem is that my data is too huge to fix this problem manually by editting. Is there any way to get the YYYY-mm-dd out of mm.dd.YY ?
Here you go...
$date = "20.02.71"; // sample date... (common German format)
$date = DateTime::createFromFormat('d.m.y', $date);
echo $date->format('Y-m-d');
will result in:
1971-02-20
Create a DateTime object, then format it to anything you want...
Well you can replace the . by -, you could do something like the following:
$date = str_replace(".", "-", "mm.dd.YY")
This would return
mm-dd-YY
You could use date_parse_from_format which would convert any formate into the formate you specify.
date_parse_from_format("y-m-d", $date);
It returns an array with very useful information like month, year etc.
While converting date format from mm-dd-yy hh:ii:ss to yy-mm-dd hh:ii:ss format using below code.
<?php
echo $start_date = date("Y-m-d H:i:s",strtotime("10-14-2015 00:00:00"));
?>
But the result is
1970-01-01 05:30:00
If it is not a proper way to use date ,provide me alternate way
First check this answer whats the difference between dates over here and simply use str_replace like as
echo $start_date = date("Y-m-d H:i:s",strtotime(str_replace("-","/","10-14-2015 00:00:00")));
Or you can also use DateTime::createFromFormat over like as
$date = DateTime::createFromFormat("m-d-Y H:i:s","10-14-2015 00:00:00");
echo $date->format('Y-m-d');
Demo
Read the manual: http://php.net/manual/en/function.strtotime.php
and also specify your PHP version.
It has a nice example on checking if strtotime conversion was successfull:
if (($timestamp = strtotime($str)) === false) {
And this conversion is very critical, it supports only limited number of formats that should be specified very precisely in order days/months/years, and separators / or - or :
So you have to pick the format that you will support from following list:
http://php.net/manual/en/class.datetime.php
Be sure to create culturally aware code (e.g. US/UK/... format)
Please write your code as below:
echo $start_date = date("Y-m-d H:i:s",strtotime("10/14/2015 00:00:00"));
I am building a timestamp from the date, month and year values entered by users.
Suppose that the user inputs some wrong values and the date is "31-02-2012" which does not exist, then I have to get a false return. But here its converting it to another date nearby. Precisely to: "02-03-2012"..
I dont want this to happen..
$str = "31-02-2012";
echo date("d-m-Y",strtotime($str)); // Outputs 02-03-2012
Can anyone help? I dont want a timestamp to be returned if the date is not original.
You might look into checkdate.
That's because strtotime() has troubles with - since they are used to denote phrase like -1 week, etc...
Try
$str = '31-02-2012';
echo date('d-m-Y', strtotime(str_replace('-', '/', $str)));
However 31-02-2012 is not a valid English format, it should be 02-31-2012.
If you have PHP >= 5.3, you can use createFromFormat:
$str = '31-02-2012';
$d = DateTime::createFromFormat('d-m-Y', $str);
echo $d->format('d-m-Y');
You'll have to check if the date is possible before using strtotime. Strtotime will convert it to unix date meaning it will use seconds since... This means it will always be a date.
You can workaround this behavior
<?php
$str = "31-02-2012";
$unix = strtotime($str);
echo date('d-m-Y', $unix);
if (date('d-m-Y', $unix) != $str){
echo "wrong";
}
else{
echo date("d-m-Y", $unx);
}
or just use checkdate()
Use the checkdate function.
$str = "31-02-2012";
$years = explode("-", $str);
$valid_date = checkdate($years[1], $years[0], $years[2]);
Checkdate Function - PHP Manual & Explode Function - PHP Manual
Combine date_parse and checkdate to check if it's a valid time.
<?php
date_default_timezone_set('America/Chicago');
function is_valid_date($str) {
$date = date_parse($str);
return checkdate($date['month'], $date['day'], $date['year']);
}
print is_valid_date('31-02-2012') ? 'Yes' : 'No';
print "\n";
print is_valid_date('28-02-2012') ? 'Yes' : 'No';
print "\n";
Even though that date format is acceptable according to PHP date formats, it may still cause issues for date parsers because it's easy to confuse the month and day. For example, 02-03-2012, it's hard to tell if 02 is the month or the day. It's better to use the other more specific date parser examples here to first parse the date then check it with checkdate.
I'm having date 20/12/2001 in this formate . i need to convert in following format 2001/12/20 using php .
$var = explode('/',$date);
$var = array_reverse($var);
$final = implode('/',$var);
Your safest bet
<?php
$input = '20/12/2001';
list($day, $month, $year) = explode('/',$input);
$output= "$year/$month/$day";
echo $output."\n";
Add validation as needed/desired. You input date isn't a known valid date format, so strToTime won't work.
Alternately, you could use mktime to create a date once you had the day, month, and year, and then use date to format it.
If you're getting the date string from somewhere else (as opposed to generating it yourself) and need to reformat it:
$date = '20/12/2001';
preg_replace('!(\d+)/(\d+)/(\d+)!', '$3/$2/$1', $date);
If you need the date for other purposes and are running PHP >= 5.3.0:
$when = DateTime::createFromFormat('d/m/Y', $date);
$when->format('Y/m/d');
// $when can be used for all sorts of things
You will need to manually parse it.
Split/explode text on "/".
Check you have three elements.
Do other basic checks that you have day in [0], month in [1] and year in [2] (that mostly means checking they're numbers and int he correct range)
Put them together again.
$today = date("Y/m/d");
I believe that should work... Someone correct me if I am wrong.
You can use sscanf in order to parse and reorder the parts of the date:
$theDate = '20/12/2001';
$newDate = join(sscanf($theDate, '%3$2s/%2$2s/%1$4s'), '/');
assert($newDate == '2001/12/20');
Or, if you are using PHP 5.3, you can use the DateTime object to do the converting:
$theDate = '20/12/2001';
$date = DateTime::createFromFormat('d/m/Y', $theDate);
$newDate = $date->format('Y/m/d');
assert($newDate == '2001/12/20');
$date = Date::CreateFromFormat('20/12/2001', 'd/m/Y');
$newdate = $date->format('Y/m/d');