29 May 2011

Example Date and strtotime to PHP

PHP, Webdeveloper No Comments

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:

2024-04-29 08:35:28

 

Month Year

echo date("F Y");

Result:
April 2024

Previous month end date

echo date('Y-m-t', strtotime('-1 month') );

Result:
2024-03-31

Previous month start date

echo date('Y-m-d', strtotime('first day of last month'));

Result:
2024-03-01

Current date + Hours, Minutes, Seconds

echo date('Y-m-d H:i:s', strtotime('now'));

Result:
2024-04-29 08:35:28

Current month last date

echo date('Y-m-t');

Result:
2024-04-30

Last day of previous 12 Months

echo date('Y-m-d', strtotime('last day of last year'));

Result:
2023-04-30

Monday this week, start of this week

echo date('Y-m-d', strtotime('monday this week'));

Result:
2024-04-29

Monday last week, start of last week

echo date('Y-m-d', strtotime('monday last week'));

Result:
2024-04-22

Sunday last week, end of last week.

echo date('Y-m-d', strtotime('sunday last week'));

Result:
2024-04-28

First day of current calendar year

echo date('Y-m-d', strtotime('first day January'));

Result:
2024-01-30

First day of last calendar year

echo date('Y-m-d', strtotime('first day January last year'));

Result:
2023-01-30

Last day of previous calendar year

echo date('Y-m-d', strtotime('last day December last year'));

Result:
2023-12-28

First day of next calendar year

echo date('Y-m-d', strtotime('last day December next year'));

Result:
2025-12-28

This week last year. Monday date, week start

echo date('Y-m-d', strtotime('Monday last week last year'));

Result:
2023-04-22

Todays date last year

echo date('Y-m-d', strtotime('-1 year'));

Result:
2023-04-29

First day of current month

echo date('Y-m-d', strtotime('first day this month '));

Result:
2024-04-30

Last day of current month

echo date('Y-m-d', strtotime('last day this month '));

Result:
2024-04-28

Last day of current year

echo date('Y-m-d', strtotime('last day of December'));

Result:
2024-12-31

First day of current month last year

echo date('Y-m-d', strtotime('first day of this month last year'));

Result:
2023-04-01

Last day of the current month last year

echo date('Y-m-d', strtotime('last day of this month last year'));

Result:
2023-04-30

First day 6 months ago

echo date('Y-m-d', strtotime('first day of -6 month'));

Result:
2023-10-01
No Responses to “Example Date and strtotime to PHP”

Leave a Reply