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));
Related
I have a some dates formatted as string in this format: 18-04-17.
I want to convert them to dates.
At first I used strtotime() to change it to date:
$timestamp = strtotime($row['reqEndDate']);
$newdate = date("d-m-Y", $timestamp);
echo $newdate;
This outputs: 17-04-2018. As you can see it mistakes the day with the year.
Next I tried to use datetime, as follows:
$newdate = datetime::createFromFormat("d-m-Y", $row['reqEndDate']);
echo $newdate->format('d-m-Y');
This outputs: 18-04-0017 .This method gets the day correctly, but instead of 2017 it prints 0017.
Is there any reason for this behavior? Maybe some settings in my php setup to look for?
Uppercase Y will produce 4 digit year. Lowercase y produces a two-digit year. Your code with DateTime should be:
$date = DateTime::createFromFormat('d-m-y', $row['reqEndDate']);
echo $date->format('d-m-Y');
You can read about supported formats in PHP's manual page about date.
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'm trying to format a date in the form mm-dd-yyyy to the form yyyy-mm-dd, however, when I try formatting it, it comes out as 1969-12-31.
Here's my code:
$custom_date = "10-13-2013";
$formatted_date = date("Y-m-d", strtotime($custom_date));
What's wrong?
$custom_date = "10-13-2013";
$formatted_date = DateTime::createFromFormat("m-d-Y", $custom_date)->format("Y-m-d");
mm-dd-yyyy is not a format that is recognised by strtotime. That's because it wouldn't reliably be able to handle dates like 03-04-2013, it is the fourth of March or the third of April?
You need to parse it manually, or use the DateTime class.
list($m,$d,$y) = explode("-",$_GET['date']);
$timestamp = mktime(0,0,0,$m,$d,$y);
$formatted_date = date("Y-m-d",$timestamp);
This is my date format:
d.m.y
but I need it like dd.mm.yy
How can with php check format and if is d.m.y then convert it to dd.mm.yy and if is already dd.mm.yy leave it.
If is y-m-d conver it to yy-mm-dd
How can i do with regex?
Why regex. Because my datum is CONSTANT. And for each installation is different. But for some cases must be not d.m.y but dd.mm.yy for example.
but can be some other format also.
$show_date = DateTime::createFromFormat('d.m.Y', $dateInput)->format('Y-m-d');
DateTime
You can use
$date = date('d.m.y', strtotime($your_date))
or
$date = new DateTime($your_date);
echo $date->format('d.m.y');
Try this :
$date = new DateTime('2000-01-01'); /// you need to convert you input date to this format
echo $date->format('d-m-y');
I have several dates being outputted into variables. They are formatted as follows:
/Date(1341788400000+0100)/
How would I go about formatting them using PHP into:
DD/MM/YYYY HH:MM
Thanks!
I ended up using the following, as the initial format was in milliseconds:
$date = 1341788400000+0100;
$date = ( $date / 1000 );
$date = date("d/m/Y H:m", $date);
$date = 1341788400000+0100;
echo date("Y/m/d H:m",$date);
Unless the +0100 is the actual time of the day (01:00) ?
First, you parse it, e.g. using strtok() http://php.net/manual/en/function.strtok.php
Then parse it as a number.
$seconds = intval($a)
Then format it using
date("Y/m/d H:m", $seconds)`.