converting string to date in php not giving exact year [duplicate] - php

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
can somebody help me with my problem? I already figure out the problem but seems i do not now how to solve it without using explode(), so the problem is that the string date for example '02-15-17' i want to convert it in date() expected result (Feb 15, 2017) using strtotime() doesn't give me a right/exact year cause of the parameter 3 which is the year it has only 2 digit in HTML5 input tag date in chrome browser, can somebody help me to solve this problem? for the vision of my code here it is.
<?php
$user_date = '02-15-17';
echo date('M d, Y', strtotime($user_date));
?>
result:
Feb 2, 1917

You could also use a DateTime with the format function:
$user_date = '02-15-17';
$dateTime =DateTime::createFromFormat('m-d-y', $user_date);
echo $dateTime->format('M d, Y');
That will give you:
Feb 15, 2017
Output php

Since all your date are over 2000's use substr function try this
$olddate = "02-15-17";
$standarddate = substr($olddate,3,2) . "." . substr($olddate,0,2) . "." . "20" . substr($olddate,6,2);
echo date("jS F, Y", strtotime($standarddate)).' - '.$standarddate;
output : 15th February, 2017 - 15.02.2017

Related

php date() method is showing wrong date [duplicate]

This question already has an answer here:
strtotime() with only year return wrong data
(1 answer)
Closed 2 years ago.
I have this PHP code:
<?php
$date = 2022;
$str = strtotime(2022);
echo $str; // 1613161320
echo '<hr/>';
echo date('Y', $str); // 2021
why this link date('Y', $str); is showing 2021 instead of 2022 ?
strtotime() expects a formated date string like 10 September 2000 see php docs.
When you want to get a unix timestamp by seperate year, month, day, hour, minute and second values you should have a look at mktime() see php docs.
So what you want to get the first 2022-date possible is:
$str = mktime(0, 0, 0, 1, 1, 2022);

PHP Display Date Format as UK not US [duplicate]

This question already has answers here:
Convert a date format in PHP [duplicate]
(18 answers)
Closed 2 years ago.
In my database I have 10-01-2021 (d/m/Y)
I want to display this on the website as (d F Y) "10 January 2021. However it shows as 1 October 2021
How do I tell the code that the day and the month are not in US order of month/day but actually as day/month?
I have tried using $projectCreated = DateTime::createFromFormat('d/M/Y', $projectCreated); but this doesn't echo anything.
$projectCreated = date_create($projectCreated);
$projectCreated = date_format($projectCreated,"d F Y");
You should echo formatted output instead the static class function.
$date = DateTime::createFromFormat('d-m-Y', '10-01-2021');
echo $date->format('d F Y');

How to remove separator in date? [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 3 years ago.
how to remove the separator from the date input I have below
$date=$_POST["input"];
$new = date_format($date,"Y M D");
Try this one
<?php
$date=$_POST["input"];
// prints the current time in date format
echo date("Y M D", strtotime($date))."\n";
?>
Use date_create() to change datatype from string to date
$date=date_create($_POST["input"];);
echo date_format($date,"Y M D");
Use strtotime function convert date string into unix timestamp. Then convert into required format.
<?php
echo date("Y M D", strtotime($_POST["input"]));
?>

How to get only date from multi string [duplicate]

This question already has answers here:
How to reformat date in PHP?
(9 answers)
Closed 4 years ago.
I have sting like this:
"Last Updated Date is Feb 12, 2011"
I want to take only date value from this string in different format like
"Y-m-d"
strpos gives us the position upto is which 18, as we do not like to include is itself, we find its lenth using strlen. Using substr, we extract the data after the position from the word is. Since we don't want extra space, trim the result and Format it using createFromFormat:
$str = "Last Updated Date is Feb 12, 2011";
$mydate = substr($str, strpos($str, 'is')+strlen('is'));
$date = DateTime::createFromFormat('M d, Y', trim($mydate));
echo $date->format('Y-m-d');
a short version for extracting the date:
substr($str, -12);

How can I convert timstamp to a readable time? [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
How can I convert 2016-12-20T18:36:14.000Z to a readable date/time in PHP?
like Dec 20, 2016.
$olddate = '2016-12-20T18:36:14.000Z';
echo date('M d,Y',strtotime($olddate));
Use the DateTime::format() like this:
<?php
echo date_create('2016-12-20T18:36:14.000Z')->format('M d, Y');
output:
~$ php test.php
Dec 20, 2016l
Try this:
date('mdY') == date('mdY', strtotime($timestamp))
Following will do the trick
$t = "2016-12-20T18:36:14.000Z";
echo date('M d,Y', strtotime($t));

Categories