Is there any way, in PHP, to automatically include a particular date in a page, which is always the date of the last Thursday please ?
Enter the magic of [fphp]strtotime[/fphp]! :magic:
<?php
echo date('r', strtotime('last Thursday'));
#Thu, 11 Nov 2010 00:00:00 +0000
?>
Hi Anthony, many thanks for the quick help. This code would always be for the last Thursday, whenever that might be. So, not always the 11th of November.
Yep.
Thanks Anthony, it’s appreciated. That’s a very clever bit of code !
Trying to get it to arrive at the format of :
15th of November 2010
am trying :
<?php
echo date(‘r’, strtotime(‘last Thursday’));
#Thursday, 11th of November 2010
?>
and that didn’t work, did look through the link as well, but couldn’t seem to find it there either.
Dez, the first argument of of the [fphp]date[/fphp] function is the date format string. (in this case ‘r’). You pass in a string to get the date in the format you wish. List of format string here.
e.g.
Y-m-d is 2010-11-11
j F is 11 November.
Thanks, so to get a format of - 11th of November 2010 (with the ‘th’), what exact format would I need please ? Did look through both links and tried
<?php
echo date(‘r’, strtotime(‘last Thursday’));
#jS \of F Y
?>
and that didn’t work either :sick:
Maybe this will clear things up.
<?php
echo date('r', strtotime('last Thursday'));
#Thu, 11 Nov 2010 00:00:00 +0000
echo date('jS \\of F Y', strtotime('last Thursday'));
#11th of November 2010
echo date(DATE_ATOM, strtotime('last Thursday'));
#2010-11-11T00:00:00+00:00
?>
Can you see what part we’re changing?
The # is just a comment and does not affect the code or output at all. It is merely there to demonstrate the output to you.
Aaaah, many thanks Anthony, much appreciated. I kept seeing the bit after the # and I couldn’t work out why it was there
Problem all solved !
Actually, and it’s probably beyond the powers of this coding, but is it possible, to do a similar function, in exactly the same date format, but where it gives a day and date from two weekdays ago, (ie, it always excludes Saturdays and Sundays) ? ?
So, 2 working days ago?
Yep, that would be it, Anthony.
ah, a very rough example would be something like…
<?php
function two_workdays_prior($from = null){
if(null === $from){
$date = strtotime('-2 days');
}else{
$date = strtotime('-2 days', (int)$from);
}
switch(date('D', $date))
{
case 'Sat':
$date = strtotime('-1 days', $date);
break;
case 'Sun':
$date = strtotime('-2 days', $date);
break;
default:
break;
}
return $date;
}
echo date('r', two_workdays_prior());
#Fri, 12 Nov 2010 21:26:42 +0000
echo date('r', two_workdays_prior(strtotime('-4 days')));
#Wed, 10 Nov 2010 21:31:45 +0000
?>
Anthony, you’re an absolute blinkin’ gem Problem now definitely solved. Funnily enough, it was what I wanted from the start really, but never thought it was possible, so didn’t think to ask. Darn clever stuff this PHP malarkey !
Many, many thanks for your patience and help - much appreciated.
Ha, you’re always welcome.
You know where I am if needs be.
Anthony.
// Works correctly as of PHP 5.2.7
echo date('r', strtotime('-2 weekdays'));
Woah…
strtotime is magic. Apparently these work too:
echo date('r', strtotime('When did I last go to the dentist?'));
echo date('r', strtotime('my birthday'));
echo date('r', strtotime('the day my car insurance is due'));
Many thanks Salathe - this looks leaner, so will go with this one. The helps much appreciated.