
123 Cal.Rptr.2d 700 (2002)
28 Cal.4th 1050
51 P.3d 913
The PEOPLE, Plaintiff and Respondent,
v.
Dale Howard JOHNSON, Defendant and Appellant.
No. S097755.
Supreme Court of California.
August 19, 2002.
*702 Gordon B. Scott, under appointment by the Supreme Court, Santa Rosa, for Defendant and Appellant.
Bill Lockyer, Attorney General, David P. Druliner and Robert R. Anderson, Chief Assistant Attorneys General, Ronald A. Bass, Assistant Attorney General, Laurence K. Sullivan and Seth K. Schalit, Deputy Attorneys General, for Plaintiff and Respondent.
*701 KENNARD, J.
Penal Code section 2900.5 provides that the total number of days a defendant spends in custody, either before sentencing or as a condition of probation, "shall be credited" against the defendant's "term of imprisonment." In this case, we resolve two issues regarding that statute: First, we conclude that a defendant can expressly waive entitlement to credits for time served. Second, we hold that such an express waiver is proper and enforceable when a trial court in sentencing a defendant to a maximum term of imprisonment conditions a grant of probation upon the defendant's waiver of custody credits.

I. FACTS
Defendant was charged with two counts of burglary and one count of receiving stolen property. Under a plea bargain, defendant entered a plea of no contest to one count of residential burglary (Pen. Code, §§ 459/460)[1] in return for dismissal of the other charges.
Because the burglary involved "significant planning" by defendant, who had copied a house key entrusted to the cleaning business where he worked, and because the value of the items taken exceeded $12,000, the trial court found that aggravating factors predominated and sentenced defendant to the upper term of six years in state prison. (See § 461, subd. 1.) The trial court further found that defendant, who was then 24 years old and had a drug problem but no prior felony convictions, would benefit from probation. The court suspended execution of sentence and placed defendant on probation for 36 months. The court conditioned its grant of probation upon defendant's agreement to (1) incarceration in county jail until space became available at Turning Point, a residential drug treatment facility, and (2) giving up entitlement to section 2900.5 custody credits, that is, the credits defendant would otherwise receive for the time served in county jail (before and after sentencing) and at Turning Point, in the event defendant were to violate probation and be sent to state prison.
The trial court told defendant: "You make the choices. If you choose to work hard in the program and get something out of it and stay off drugs, you will never have to come back in here, and you're not going to have a problem. If you don't do that, you will come back in here, and you are going to go to prison. So use a six-year [state prison] sentence as a motivating *703 tool to help you make the right choice when you are faced with choices. I am going to give you the opportunity."
Although defendant expressly agreed to a waiver of custody credits, his attorney, presumably to preserve the issue for appeal, "technically object[ed]" for the record. On appeal, defendant argued that the waiver of custody credits was not enforceable because the trial court had imposed the maximum term of imprisonment and thus if defendant violated probation and was sent to state prison, his total time in custody would exceed that allowed by law. The Court of Appeal affirmed the trial court, and we granted defendant's petition for review.

II. DISCUSSION
Under section 2900.5, a defendant sentenced either to county jail or to state prison is entitled to credit against the term of imprisonment for days spent in custody before sentencing as well as those served after sentencing as a condition of probation. (In re Rojas (1979) 23 Cal.3d 152, 156, 151 Cal.Rptr. 649, 588 P.2d 789; accord, People v. Bruner (1995) 9 Cal.4th 1178, 40 Cal.Rptr.2d 534, 892 P.2d 1277.) This provision also applies to custodial time in a residential treatment facility. (§ 2900.5.)[2]
Nearly 25 years ago, Justice Bernard Jefferson's concurrence in In re Chamberlain (1978) 78 Cal.App.3d 712, 720, 144 Cal.Rptr. 326 (Chamberlain) pointed out that nothing in the language of section 2900.5 prohibited a defendant from knowingly and intelligently waiving entitlement to custody credits.
In Chamberlain, the trial court suspended a state prison sentence and placed the defendant on probation conditioned upon service of one year in county jail with no credit for the days the defendant had already spent in jail before sentencing. In a petition for habeas corpus filed in the Court of Appeal, the defendant unsuccessfully challenged that order. In a concurring opinion, Justice Jefferson agreed with the majority's denial of relief, but he did so for the following reason: By accepting probation, the defendant had waived his right to custody credits under section 2900.5, and he had done so knowingly and intelligently. But absent such a waiver, Justice Jefferson explained, any period of incarceration without credits would be an illegal sentence under former section 19a (now § 19.2), which "places a one-year limit upon a county jail commitment given as a condition of probation." (Chamberlain, supra, 78 Cal.App.3d at pp. 720-721, 144 Cal.Rptr. 326 (cone. opn. of Jefferson, J.).)
Agreeing with that view, the Court of Appeal in People v. Johnson (1978) 82 Cal.App.3d 183, 188, 147 Cal.Rptr. 55 (Johnson) held that section 2900.5 permits a defendant to "knowingly and intelligently waive" the right to receive credit for "all days of custody [to] be credited to [the defendant's] sentence, including any period of imprisonment as a condition of probation." The court described its holding as a "realistic interpretation" of section 2900.5, which allows a trial judge in a "proper case," acting with the defendant's consent, *704 "[to] formulate a sentence which fits the crime and the criminal." (Johnson, supra, at p. 188, 147 Cal.Rptr. 55.) The court also noted that when a defendant violates probation after spending one year in county jail as a condition of probation, its holding would mean that the sentencing judge would not have to "choose between ignoring the violation or imposing sentence to state prison." (Ibid.) Rather, the judge could "fashion an intermediate disposition by modifying probation" to provide for additional time in county jail. (Ibid.)
Since then, Courts of Appeal, in upholding custody credits waivers in a wide variety of circumstances, have not questioned that a defendant may waive entitlement to such credits under section 2900.5. (See People v. Torres (1997) 52 Cal.App.4th 771, 775, 60 Cal.Rptr.2d 803 [waiver at sentencing of future credits for probationary term in drug treatment program]; People v. Salazar (1994) 29 Cal.App.4th 1550, 1553-1556, 35 Cal.Rptr.2d 221 [waiver at reinstatement of probation of past custody credits]; People v. Ambrose (1992) 7 Cal. App.4th 1917, 1920, 1925, 9 Cal.Rptr.2d 812 [waiver at reinstatement of probation of past and future credits]; People v. Zuniga (1980) 108 Cal.App.3d 739, 743, 166 Cal.Rptr. 549 [waiver at sentencing of past custody credits].) In People v. Tran (2000) 78 Cal.App.4th 383, 92 Cal.Rptr.2d 815 (Tran), which we address later in this opinion, the Court of Appeal set aside a waiver of custody credits by a defendant sentenced to a suspended maximum term in state prison, but the court still acknowledged that defendants could waive custody credits in situations not involving a suspended sentence for the maximum term.
Like the Courts of Appeal that have addressed the issue, we too conclude that a defendant may expressly waive entitlement to section 2900.5 credits against an ultimate jail or prison sentence for past and future days in custody. As the United States Supreme Court has observed, "`[t]he most basic rights of criminal defendants are ... subject to waiver.'" (United States v. Mezzanatto (1995) 513 U.S. 196, 201, 115 S.Ct. 797, 130 L.Ed.2d 697.) This is consistent with the well-established rule allowing "`[a] party [to] waive any provision ... intended for his benefit.'" (Ibid.; accord, Civ.Code, § 3513; Cowan v. Superior Court (1996) 14 Cal.4th 367, 371, 58 Cal.Rptr.2d 458, 926 P.2d 438.) As with the waiver of any significant right by a criminal defendant, a defendant's waiver of entitlement to section 2900.5 custody credits must, of course, be knowing and intelligent. (People v. Harris (1987) 195 Cal.App.3d 717, 725, 240 Cal.Rptr. 891.) Because a defendant may give up the statutory right to custody credits, a trial court has discretion to condition a grant or extension of probation upon a defendant's express waiver of past and future custody credits.[3]
Defendant acknowledges that trial courts generally have authority to condition probation upon a waiver of credits for time in custody. But he contends that in this case the trial court exceeded that authority because, after imposing the maximum prison term for burglary (six years), it required, as a condition of granting probation, that defendant give up entitlement to custody credits both for time in county jail and at Turning Point, the drug treatment *705 facility. As a consequence, defendant points out, were he to violate probation, he would face the full six-year prison term with no reduction for time spent in county jail or at Turning Point, resulting in a total time in custody in excess of the maximum state prison term for the offense.
In support, defendant relies on People v. Ambrose, supra, 7 Cal.App.4th 1917, 9 Cal. Rptr.2d 812 (Ambrose). That reliance is misplaced, however.
In Ambrose, the Court of Appeal upheld the trial court's requirement, imposed as a condition of probation, that the defendant waive future custody credits for time spent at Diablo Valley Ranch, an alcohol treatment facility. As in this case, the defendant in Ambrose was sentenced to the upper term, execution of which was suspended. In upholding the defendant's waiver of custody credits, the Court of Appeal in Ambrose observed: "This does not mean, of course, that we would endorse a situation in which the denial of future credits enabled the court to impose a term which is longer than the maximum allowable for the offense. Even if a defendant agrees to such a negotiated disposition, a trial court should not impose a sentence in excess of the maximum allowable for the plea entered. Appellant would be entitled to discharge from custody at the ranch if and when his total custodial time, including time at the ranch, equaled the maximum incarceration period." (Ambrose, supra, 7 Cal.App.4th at p. 1924, 9 Cal.Rptr.2d 812, italics added.)
That comment by the Ambrose court envisioned a situation in which a trial court, as a condition of probation, has imposed an indefinite custodial term in a residential treatment program. Ambrose suggests that when the total time the defendant spends in county jail and in the residential program equals the crime's maximum sentence, the defendant must be discharged from the program. But Ambrose did not address the issue here: whether imposition of a suspended maximum prison term precludes the trial court from requiring, as a condition of probation, that the defendant waive entitlement to section 2900.5 custody credits. Thus, it is not authority for defendant's contention here.
Invoking the rule that probation conditions must serve some legitimate purpose relating to the offender or the offense (see People v. Carbajal (1995) 10 Cal.4th 1114, 1121, 43 Cal.Rptr.2d 681, 899 P.2d 67; People v. Lent (1975) 15 Cal.3d 481, 486, 124 Cal.Rptr. 905, 541 P.2d 545), defendant asserts that the only purpose served by conditioning his probation upon a waiver of past and future custody credits was an illegitimate oneexposing him to the possibility of serving more than six years in custody for an offense with a six-year maximum sentence. We disagree.
Here, after accepting defendant's waiver of custody credits, the trial court placed him on probation for 36 months conditioned upon successful completion of the Turning Point residential drug treatment program. As noted in Ambrose, supra, 7 Cal.App.4th at page 1925, 9 Cal. Rptr.2d 812, when probation is conditioned upon completion of a residential treatment program, custody credit waivers ensure the defendant's "optimum chances of success in [the] treatment program, while reserving an appropriate sentence if, despite the opportunity received, the treatment program and probation are not completed." Here, the trial court required defendant's waiver not just of credits for time spent at the Turning Point treatment program but also for time spent in county jail, both before sentencing and while awaiting placement in the program. The waiver was to give defendant an incentive *706 to successfully complete the residential treatment program based on the knowledge that failure to do so would expose him to imposition of the six-year prison sentence unreduced by previously served custodial time. Here, defendant, who admittedly suffers from drug dependency, committed a serious residential burglary warranting maximum punishment, but the trial court's grant of probation gave him a chance to get off drugs and to avoid state prison. On these facts, we cannot say that the trial court's requirement of a waiver of custody credits as a condition of granting probation lacked any legitimate penal function.
We now address the Court of Appeal's decision in Tran, supra, 78 Cal.App.4th 383, 92 Cal.Rptr.2d 815, which held that waivers of custody credits are improper when a trial court suspends a maximum state prison sentence. We find Tran's reasoning unpersuasive.
In Tran, the defendant deposited bad checks in three bank accounts and then withdrew $40,000 to cover gambling debts. He later pleaded guilty to three counts of grand theft. The trial court imposed the maximum prison sentence, suspended its execution, and placed the defendant on five years' probation conditioned upon an 18-month county jail term, a waiver of future custody credits, and restitution to the banks of the full $40,000. Under protest, the defendant agreed to waive entitlement to credit for days in custody. On appeal, he challenged that aspect of the trial court's sentencing order as unauthorized, arguing that if he violated probation and was sent to state prison with no credit for the 18-month jail term, his total custodial time would exceed the maximum prison term. The Court of Appeal agreed. (Tran, supra, 78 Cal.App.4th 383, 385-390, 92 Cal.Rptr.2d 815.)
We disagree with Tran, supra, 78 Cal.App.4th 383, 92 Cal.Rptr.2d 815, that a trial court, after imposing and suspending a state prison sentence for the maximum term allowed by law, may never condition a grant of probation on a waiver of the custody credits authorized by section 2900.5. In general, defendants may waive provisions that are intended for their benefit, and the provision of section 2900.5 allowing defendants to receive credits against an eventual sentence for time spent in custody before sentencing or while on probation is intended to benefit defendants, as we explained at 123 Cal. Rptr.2d page 704, 51 P.3d page 916, ante.[4]
Exercising its discretion, the trial court determined that imposing the maximum term for defendant's crime of residential burglary and conditioning probation on a waiver of section 2900.5 custody credits was necessary to provide defendant with sufficient incentive to comply with the other terms of probation and to successfully complete the rehabilitation program. Defendant makes no claim that, as a result of the waiver condition, the total time he will be required to spend in custody before sentencing and as a condition of probation will exceed the maximum term statutorily authorized for the crime he committed. Under these circumstances, we conclude that defendant has failed to show that the custody credit waiver condition was invalid, and we further conclude that the state prison sentence the trial court imposed, for the maximum term authorized by law, was a valid sentence and *707 was not rendered unlawful by defendant's waiver of custody credits.

DISPOSITION
We affirm the judgment of the Court of Appeal.
WE CONCUR: GEORGE, C.J., BAXTER, WERDEGAR, CHIN, BROWN and MORENO, JJ.
NOTES
[1]  Further undesignated statutory references are to the Penal Code.
[2]  Subdivision (a) of section 2900.5 provides: "In all felony and misdemeanor convictions, either by plea or by verdict, when the defendant has been in custody, including, but not limited to, any time spent in a jail, camp, work furlough facility, halfway house, rehabilitation facility, hospital, prison, juvenile detention facility, or similar residential institution, all days of custody of the defendant, including days served as a condition of probation.... shall be credited upon his or her term of imprisonment.... If the total number of days in custody exceeds the number of days of the term of imprisonment to be imposed, the entire term of imprisonment shall be deemed to have been served."
[3]  We do not here consider whether a trial court could within the proper exercise of its discretion routinely condition grants of probation upon waivers of credit for time served. (Compare People v. Penoli (1996) 46 Cal. App.4th 298, 303-304, 53 Cal.Rptr.2d 825 [abuse of discretion for trial court as a "standard practice" to require custody credit waivers for time in drug rehabilitation programs] with People v. Torres, supra, 52 Cal.App.4th 771, 775-783, 60 Cal.Rptr.2d 803 [disagreeing with People v. Penoli ].)
[4]  We disapprove People v. Tran, supra, 78 Cal.App.4th 383, 92 Cal.Rptr.2d 815, to the extent it is inconsistent with our conclusion here.
