Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove leading zeros from abbreviated date with PHP

Tags:

date

php

zero

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.

like image 868
HandiworkNYC.com Avatar asked Mar 19 '10 19:03

HandiworkNYC.com


People also ask

How to remove leading 0 in PHP?

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.

How can I add zero in front of a number in PHP?

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.


2 Answers

<?php

$date = "01.08.86";
$unix = strtotime($date);
echo date('n.j.y', $unix);
like image 75
Sean Fisher Avatar answered Oct 01 '22 08:10

Sean Fisher


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'
like image 29
codaddict Avatar answered Oct 01 '22 07:10

codaddict