Re: Adding zeros to date
| From: | Morgan Curley | Date: | Fri, 05 Oct 2001 12:36:36 +0000 |
| Subject: | Re: Adding zeros to date | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-70076@lists.php.net to get a copy of this message | ||
Why not just:
function paddate( $value ){
if( $value < 10 ){
$value = "0$value";
}
return $value;
}
$date_time_array = getdate (time());
$date = paddate( $date_time_array[ "mday"] );
$month = paddate( $date_time_array[ "mon"] );
$year = paddate( $date_time_array[ "year"] );
$datemonth = $year . $month . $date;
echo $datemonth;
Morgan
At 01:04 PM 10/5/2001 +0000, you wrote:
>if you can't do it with date() use
>substr("0".$month, -2, 2)
>
>On Friday 05 October 2001 09:44, Daniel Alsén wrote:
>> Hi,
>>
>> is there a easier way to add zeros to date than the script below? (ie to
>> get 20011005 instead of 2001105). I wrote a long string replace. But it
>> seems kind of unecessary to me. Is it?
>>
>>
>> $date_time_array = getdate (time());
>> $date = $date_time_array[ "mday"];
>> $month = $date_time_array[ "mon"];
>> $year = $date_time_array[ "year"];
>>
>>
>> if ($month < 10) {
>> $month = str_replace("1", "01", $month);
>> $month = str_replace("2", "02", $month);
>> $month = str_replace("3", "03", $month);
>> $month = str_replace("4", "04", $month);
>> $month = str_replace("5", "05", $month);
>> $month = str_replace("6", "06", $month);
>> $month = str_replace("7", "07", $month);
>> $month = str_replace("8", "08", $month);
>> $month = str_replace("9", "09", $month);
>> }
>>
>> if ($date < 10) {
>> $date = str_replace("1", "01", $date);
>> $date = str_replace("2", "02", $date);
>> $date = str_replace("3", "03", $date);
>> $date = str_replace("4", "04", $date);
>> $date = str_replace("5", "05", $date);
>> $date = str_replace("6", "06", $date);
>> $date = str_replace("7", "07", $date);
>> $date = str_replace("8", "08", $date);
>> $date = str_replace("9", "09", $date);
>> }
>>
>> $datemonth = $year . $month . $date;
>>
>> echo $datemonth;
>>
>> # Daniel Alsén | www.mindbash.com #
>> # d@mindbash.com | +46 704 86 14 92 #
>> # ICQ: 63006462 | #
>
>--
>PHP General Mailing List (http://www.php.net/)
>To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
>For additional commands, e-mail: php-general-help@lists.php.net
>To contact the list administrators, e-mail: php-list-admin@lists.php.net
-------------
Morgan Curley
Partner, e4media
mcurley@e4media.com
917 751 8328
http://www.e4media.com
-------------------------