简体   繁体   中英

Convert past date format in php

I'm upload the excel data into mysql. in there date save like 16.5.59(dd.mm.yy) formate. I try to change this using this code

$date = '16.5.59'; 
echo date('d-m-y',strtotime($date));

it always show current time like 04-02-15. Pls change this to dd-mm-yyyy formate.

Thanks in Advance.

Timestamp was limited from 01-01-1970 to 19-01-2038 on some systems (eg Windows).

If it not cover the between given range then it will take current date.

Try to make the date yourself using explode and strtotime :

date_default_timezone_set('America/Los_Angeles');

$date = '16.5.59';

$date_time = strtotime(implode('-', array_reverse(explode('.', $date))))
$date_str = date('d-m-Y', $date_time);

echo $date_str;

Output: 16-5-2059

You ARE required to specify that the year is 1959 instead of 2059 (or are you sure that it's really 2059?)

You can make change from 2059 to 1959 like this:

date_default_timezone_set('America/Los_Angeles');

$date = '16.5.59';

$date_arr = array_reverse(explode('.', $date))

# This line does the job. Make sure all years are between 1900 - 1999.
$date_arr[0] = '19' . $date_arr[0];

$date_time = strtotime(implode('-', $date_arr))
$date_str = date('d-m-Y', $date_time);

echo $date_str;

I hope this answer can help you.

This is by no means the best solution but using php's date_parse_from_format is possible. You could supply a format and use the following

$date = "16.5.59";
$dateObj = date_parse_from_format("j.n.yy", $date)

with the $dateObj you can work what you need such as :

 echo $dateObj['year'];

IDE Running Example

Worth noting you require PHP >=v5.3

 $date = '16.5.59'; $date_array = explode(".",$date); $var_day = $date_array[0]; $var_month = $date_array[1]; > Blockquote $var_year = $date_array[2]; echo $new_date_format = "$var_day-$var_month-$var_year"; 

You can do this if it is for simpler task. It will consume time.

$date = '16.5.59'; 
$dtSplit=explode(".",$date);
echo "<br>".$dtSplit[0].".".$dtSplit[1].".".$dtSplit[2];
echo "<br>".$dtSplit[0].".".$dtSplit[2].".".$dtSplit[1];
echo "<br>".$dtSplit[1].".".$dtSplit[0].".".$dtSplit[2];
echo "<br>".$dtSplit[1].".".$dtSplit[2].".".$dtSplit[0];
echo "<br>".$dtSplit[2].".".$dtSplit[0].".".$dtSplit[1];
echo "<br>".$dtSplit[2].".".$dtSplit[1].".".$dtSplit[0];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM