Parse Date in PHP - php

I need some help parsing a date in PHP to a date object. I thought this was 8601 format, but $date = DateTime::createFromFormat(DateTime::ISO8601, "2018-11-14T01:11:36.059Z") is not working.
Date Format is: 2018-11-14T01:11:36.059Z

Try This:
echo date('Y-m-d H:i:s', strtotime('2018-11-14T01:11:36.059Z'));
//Output
2018-11-13 20:11:36

Related

php date to sql

I want to store php date in sql but it store 1970-01-01 05:00: instead of input datetime
This is html form
<input type="datetime" id="eventstart" name="txtEventStart" class="form-control" value="<?php echo date('m/Y/d h:i:s');?>">
when data is sent using post
$date = $this->input->post('txtEventStart');
I convert string to date format using:
$date2 = date('Y-m-d H:i:s', strtotime($date));
It stores '1970-01-01 05:00:00'
I want to know what is the correct format for storing such type of date.
Your code is giving you the wrong results because your date is in a format (m/Y/d h:i:s) that is not recognised by strtotime. Instead, use date_create_from_format to convert it and output a date in the correct form for SQL. For example:
$date = '08/2019/19 10:23:41';
echo date('Y-m-d H:i:s', strtotime($date)) . PHP_EOL;
echo date_create_from_format('m/Y/d h:i:s', $date)->format('Y-m-d H:i:s');
Output:
1970-01-01 01:00:00
2019-08-19 10:23:41
Demo on 3v4l.org
'YYYY-MM-DD hh:mm:ss' is not an acceptable format for PHP date function. Use Y-m-d H:i:s instead. More info here: https://www.w3schools.com/php/func_date_date.asp
Update: For date input field use ISO date format Y-m-d or change input type to 'text'

Convert Time stamp to readable format

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

PHP date year format

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));

Formatting date gives 12/31/1969

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);

php format parsing and changing. Date format is always two characters

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');

Categories