How to create a timer that only increases when if command is true?

I want to create a timer that increases but only when a if command is true. How do i do that in Unity?

private bool myCondition;
private float timer;

if(myCondition)
    timer += Time.deltaTime;