How to convert year format YY into YYYY in PHP [duplicate] - php

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I need to convert year(YY) to YYYY. Here i am try to use date function in PHP but not getting the expected result.
$expiry_year = "30"; //year=2030
echo date('Y',strtotime($expiry_year)); //Result: 1970
echo date('Y',$expiry_year); //Result: 1970
Expected Result: 2030
Thanks To All!

Try this, use createFromFormat
$date = "30";
$dates = DateTime::createFromFormat('y', $date);
//now to get the outpu:
$arr = $dates->format('Y'); // output : 2030
DEMO

Try this :
$date = DateTime::createFromFormat('y', '30');
echo $date->format('Y');

Note:
If the number of the year is specified in a two digit format, the values between 00-69 are mapped to 2000-2069 and 70-99 to 1970-1999. See the notes below for possible differences on 32bit systems (possible dates might end on 2038-01-19 03:14:07).
For this you can use date_create_from_format Alias of the DateTime class createFromFormat method like as
$expiry_year = "30";
$date = date_create_from_format('y',$expiry_year);
echo $date->format('Y'); //2030
Docs

$expiry_year = "30";
$ce = substr(date('Y'),0,2); // Check the century
$ny = substr(date('Y'),2,2); // Check current year (2 digits)
if($expiry_year < $ny) {
$next_ce = $ce+1;
$output = $next_ce.$expiry_year;
} else {
$output = $ce.$expiry_year;
}
Something like that ? :D

Related

define a sting as a date and add days [duplicate]

This question already has answers here:
Adding days to $Date in PHP
(12 answers)
Closed 3 months ago.
I have a string that represent a date (ex. 01/10/2022 d/m/Y) and I want to add 90 days to this date, the problem I think I have is that php dont know how do add the 90 days to this date because is a string. I tried different solution posted by others but without a good result.
One example that I tried is:
$nextduedate = "01/10/2022";
$myDateTime = DateTime::createFromFormat('d/m/Y', $nextduedate);
$myDateTime = $myDateTime->format('d/m/Y');
$number_of_days = 90;
$str =' + '. $number_of_days. ' days';
$myDateTime = date('d/m/Y', strtotime($myDateTime. $str));
echo $myDateTime;
adn the result is 10/04/2022 istead of 01/01/2023
I know im missing something but I dont know what.
If you use Date Object, this script can help you.
You have to use "modify" function
<?
$startEntry = "01/10/2022";
$nbDays = 90;
$stringDiff = "+".$nbDays." days";
$startDate = DateTime::createFromFormat('d/m/Y', $startEntry);
$endDate = $startDate->modify($stringDiff);
echo $endDate->format('d/m/Y');
?>

PHP: convert date time variable into date [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 4 years ago.
I'm trying to convert a date and time variable into date only in a different format:
$test_date = '01/07/2018 10:00-12:00';
$result = substr($test_date, 0, 10);
$newDate = date("Ymd", strtotime($result));
echo $newDate;
Expected result= 20180701
Actual result= 20180107
Tried all the relative variations I can find on here but keep getting 'Ydm' instead of 'Ymd'.
I'm I missing something obvious here?
Looks like it's being parsed 'wrongly' because it's not explicitly stated what the source format is, using DateTime objects, you can do (not tested):
$result = substr($test_date, 0, 10);
$dateTime = DateTime::createFromFormat('d/m/Y', $result);
$newDate = $dateTime->format('Y-m-d');
echo $newDate;

From date with time to date without time in PHP [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have to convert this string date 2016-09-26 00:00:00.000 to the yyyy-mm-dd format without other characters.
Can you help me, please?
You can just use the DateTime class along with the format() method:
$d = new DateTime('2016-09-26 00:00:00.000');
echo $d->format('Y-m-d');
Try this:
$datetime = '2016-09-26 00:00:00.000';
$date = date('Y-m-d', strtotime('2016-09-26 00:00:00.000'));
you will get the only date part in 'yyyy-mm-dd' format.
$test = strtotime('2016-09-26 00:00:00.000');
$test1 = date('Y-m-d h:i', $test);
$array1 = explode(' ', $var);
$date_temp = $array1[0];

change date format from d/m/Y to Y-m-d [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
Hello i have date format d/m/Y and i like to change it to Y-m-d.
i use this code:
$check_in = '31/12/2014';
$check_out = '2/01/2015';
$md = explode("/", $check_in); // split the array
$nd = $md[2]."-".$md[1]."-".$md[0]; // join them together
$check_in_new = date('Y-m-d', strtotime($nd));
$mmd = explode("/", $check_out); // split the array
$nnd = $md[2]."-".$mmd[1]."-".$mmd[0]; // join them together
$check_out_new = date('Y-m-d', strtotime($nnd));
and its work fine, but if i try to convert 02/01/2015 (year 2015) the result is 2014-01-02
its convert the year to 2014
any help???
I suggest utilize DateTime class in this case, and provide proper format in it with createFromFormat, instead of exploding those strings:
$check_in = '31/12/2014';
$check_out = '2/01/2015';
$check_in_new = DateTime::createFromFormat('d/m/Y', $check_in);
$check_out_new = DateTime::createFromFormat('d/m/Y', $check_out);
echo $check_in_new->format('Y-m-d') . '<br/>';
echo $check_out_new->format('Y-m-d') . '<br/>';
Ref: http://php.net/manual/en/class.datetime.php
If you want to continue the way you are doing right now, you can easily do it by the following way:
$check_in = '31/12/2014';
$check_out = '2/01/2015';
$check_in_new = implode('-',array_reverse(explode('/',$check_in)));
$check_out_new = implode('-',array_reverse(explode('/',$check_out)));
But there is a better way to do it:
//Set format
$format = 'Y-m-d';
$dto = new DateTime(strtotime($check_in));
$check_in_new = $dto->format($format);

PHP date conversion mm/dd/yy to yyyy/mm/dd -error [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 9 years ago.
My input date format is dd/mm/yy. I am using the following code to convert it into yyyy-mm-dd.
$cal_date= $fileop[1];
$date = str_replace('/', '-', $cal_date);
$invdate=date('Y-m-d',strtotime($date));
Result i got is 24/10/13-->2024-10-13. Can anyone correct me.
Here two conversions 2digit year to 4 digit year
and /replaced by -dash
Using the DateTime object might be better
try{
$date = DateTime::createFromFormat('d/m/y',$cal_date);
$invdate = $date->format('Y-m-d');
} catch (Exception $e) {
echo $e->getMessage(); // or
print_r(DateTime::getLastErrors());
}
$date = DateTime::createFromFormat('d/m/y',$cal_date);
$break = explode("/", $date);
$yearComp = $break[2];
if($yearComp < 70)
$invdate = $date->format('Y-m-d');
else {
// code that would add 100 years to the date generated }
But just know that 69 would print out 2069 but 70 would be 1970. If you won't get to 70, then this should be fine
Another way - See The Demo Here
<?php
$date= "02/05/2013"; //dd/mm/yy
$dd=substr($date, 0, 2);
$mm=substr($date, 3, 2);
$yy=substr($date, 6, 4);
echo $yy."/".$mm."/".$dd; // yyyy-mm-dd.
?>
In one shoot:
$invdate = date_format(date_create_from_format('m/d/y', $fileop[1]), 'Y-m-d');
If you are not confident with the imput date format:
if ($invdate = date_create_from_format('m/d/y', $fileop[1])) {
$invdate = date_format($invdate, 'Y-m-d'); //date format yyyy-mm-dd
} else {
echo "ERROR: '{$fileop[1]}' has not the date format mm/dd/yy";
}

Categories