29
May
2011
Example Date and strtotime to PHP
Soms heb je in een webpagina een datumvermelding nodig die relatief is aan de werkelijke datum. De eerste dag van de lopende maand, bijvoorbeeld. In PHP kan je met de “date”-functie, al dan niet in combinatie met “strotime” een groot aantal van deze relatieve datumvermeldingen eenvoudig opnemen.
Algemene syntax: string date ( string $format [, int $timestamp = time() ] )
Hieronder een aantal voorbeelden.
Huidige datum:
2025-05-02 12:52:33
Month Year
echo date("F Y"); Result: May 2025
Previous month end date
echo date('Y-m-t', strtotime('-1 month') ); Result: 2025-04-30
Previous month start date
echo date('Y-m-d', strtotime('first day of last month')); Result: 2025-04-01
Current date + Hours, Minutes, Seconds
echo date('Y-m-d H:i:s', strtotime('now')); Result: 2025-05-02 12:52:33
Current month last date
echo date('Y-m-t'); Result: 2025-05-31
Last day of previous 12 Months
echo date('Y-m-d', strtotime('last day of last year')); Result: 2024-05-31
Monday this week, start of this week
echo date('Y-m-d', strtotime('monday this week')); Result: 2025-04-28
Monday last week, start of last week
echo date('Y-m-d', strtotime('monday last week')); Result: 2025-04-21
Sunday last week, end of last week.
echo date('Y-m-d', strtotime('sunday last week')); Result: 2025-04-27
First day of current calendar year
echo date('Y-m-d', strtotime('first day January')); Result: 2025-01-03
First day of last calendar year
echo date('Y-m-d', strtotime('first day January last year')); Result: 2024-01-03
Last day of previous calendar year
echo date('Y-m-d', strtotime('last day December last year')); Result: 2024-12-01
First day of next calendar year
echo date('Y-m-d', strtotime('last day December next year')); Result: 2026-12-01
This week last year. Monday date, week start
echo date('Y-m-d', strtotime('Monday last week last year')); Result: 2024-04-21
Todays date last year
echo date('Y-m-d', strtotime('-1 year')); Result: 2024-05-02
First day of current month
echo date('Y-m-d', strtotime('first day this month ')); Result: 2025-05-03
Last day of current month
echo date('Y-m-d', strtotime('last day this month ')); Result: 2025-05-01
Last day of current year
echo date('Y-m-d', strtotime('last day of December')); Result: 2025-12-31
First day of current month last year
echo date('Y-m-d', strtotime('first day of this month last year')); Result: 2024-05-01
Last day of the current month last year
echo date('Y-m-d', strtotime('last day of this month last year')); Result: 2024-05-31
First day 6 months ago
echo date('Y-m-d', strtotime('first day of -6 month')); Result: 2024-11-01
No Responses to “Example Date and strtotime to PHP”