When exactly does a checkmate or stalemate take effect? I'm not enthusiastic about chess in particular, so if I use terminology incorrectly please correct me. I use "phase 1 &amp; phase 2" for each player's move, and "turn n" for each pair of phases.

I'm a novice programmer writing a chess program as a personal challenge, and  would like to know exactly when checkmates and stalemates take effect. 

Specifically:


If a player 1's move causes checkmate or stalemate, does the game end during phase 1, or at the beginning of phase 2?


Edit:

It seems I wasn't clear enough that I wasn't looking for a programming answer, I wanted to know about chess standards. Even if my program can work without detecting the exact final phase, I would like that information to be available.

I looked at the FIDE rules as mentioned in Laska
's answer [link], and Section 5 points out what I was looking for:


5.1 a. The game is won by the player who has checkmated his opponent’s king. This immediately ends the game, provided that the move producing the checkmate
position was a legal move.
5.2 a. The game is drawn when the player to move has no legal move and his king is not in check. The game is said to end in ‘stalemate’. This immediately ends the game, provided that the move producing the stalemate position was legal.


From these rules I gather that if I cause a checkmate, the game ends before my opponent's next phase, and if I cause a stalemate, the game ends during my opponent's next phase.

Please correct me if you think I'm not understanding something correctly. Also, I know I've seen the term FIDE before but I'm not sure if their chess laws would generally be considered a widely accepted and robust standard. I figure a chess community is the right place to ask about whether or not I should be using their standards.
Take your pick.

Conventionally in computer chess, we express your idea of phases as "plies", or half-moves, where 2 plies is one "turn".

You can detect checkmate in the ply where the checkmating move is made, but it is more common for checkmate to be detected in the following ply, where your program generates legal moves for the mated player, only to find that there are 0 available.

It is often more convenient and efficient that way, as the work to generate moves would have been done anyway had the position not been mate, so no extra work is done to determine whether a position is checkmate.