Solved

php How to strip zero's from month and day date

Posted on 2010-09-04
6
348 Views
Last Modified: 2012-05-10
hi,
the code below is grabing dates from my db that I need to use in jquery datepicker.

my problem is that my dates from the db are in the wrong format that my datepicker is accepting.

right out of the db the format is "2010-09-09,2010-09-17,2010-12-05" etc

The datepicker does not like the zero's infront  of the month's or days it prefers the format below:
"2010-9-9,2010-9-17,2010-12-5"

How can I strip the zero's out of the "arrayy" array only if they exist?

thanks!

$string =  $dated["start_date"];
$fresh = substr($string, 0, -9); // removes the time and leaves date from stamp


$arrayy[]=$fresh;



}

echo "<script>";

echo "var disabledDays = new Array();";



foreach($arrayy as $key=>$value) {
 echo "disabledDays[$key] = \"$value\";";
   
}

echo "</script>";

Open in new window

0
Comment
Question by:Solutionabc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 33602911
Use (client side) : myDate.replace(/-0/g,"-");

test page :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<script language="javascript">
	var myDates = "2010-09-09,2010-09-17,2010-12-05";
	alert( myDates.replace(/-0/g,"-") );
</script>
</body>
</html>

Open in new window

0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 33602925
server side : http://www.php.net/manual/en/function.preg-replace.php

preg_replace(/-0", "-", $myDates);
<?php
    $myDates = "2010-09-09,2010-09-17,2010-12-05";;
    $myDates = preg_replace("/-0/", "-", $myDates);
    print $myDates;
?>

Open in new window

0
 
LVL 14

Expert Comment

by:shobinsun
ID: 33602929
HI,

try this example:

        $TR  = "2010-09-09";
      $st = preg_replace(array('/-0/'), ' ', $TR);
      $st1 = preg_replace(array('/ /'), '-', $st);
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 33602956
Convert your database date to unix timestamp (strtotime) and then format that using DATE - Y for 4 digit year, n for months without leading zeros, and j for days without leading zeros.





echo date("Y-n-j",strtotime($dated["start_date"]));

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 33602962
In your MySQL : select replace(myDate,"-0","-") as myDate from myTable

test page lol :



select replace("2010-09-09","-0","-") as "myDate" from dual

select replace("2010-09-09,2010-09-17,2010-12-05","-0","-") as "myDate" from dual

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 33602989
Thanks for the points!
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question