I was wondering if there is a way, using PHP, to change this date format: 01.08.86 (January 8, 1986) to this format: 1.8.86.
Given a number in string format and the task is to remove all leading zeros from the given string in PHP. Method 1: Using ltrim() function: The ltrim() function is used to remove whitespaces or other characters (if specified) from the left side of a string.
There are many ways to pad leading zeros in string format. But in case of string the best way to achieve the task is by using sprintf function and also you can use substr() function. Using sprintf() Function: The sprintf() function is used to return a formatted string.
<?php
$date = "01.08.86";
$unix = strtotime($date);
echo date('n.j.y', $unix);
How about a regex based solution:
$str = '01.08.86';
$a = array('/^0(\d+)/','/\.0(\d+)/');
$b = array('\1','.\1');
$str = preg_replace($a,$b,$str);
// $str is now '1.8.86'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With