How i can reproduce this date format with php?
2016-04-07T09:03:32
I'm using:
$date = date('Y-m-d h:i:s', time());
But i don't know what is The 'T' printed beetween date and time
What youre looking for is:
$date = date('Y-m-d\Th:i:s', time());
The 'T' is for HTML5 local date-time required format.
If you want to use this format to be sure of compatability, you should use the constant to speficy the format which includes the timezone offset between local and GMT.
$date = date(DateTime::ATOM, time());
which will output:
2016-04-07T01:20:48-07:00
The T designates the start of a time string in the ISO 8601 date format.
You can reproduce this format using date like this:
date('c')
But note this will (and should) also include the timezone offset. Because the date string will always be the same format you remove this quite easily to match your specified format with substr like so:
$date = date('c');
$formattedDate = substr($date, 0, 19);
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));
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');
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).