<

Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x

Display your dates as Today, Yesterday etc in PHP

Published on
3,795 Points
795 Views
Last Modified:
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.

 
<?php
$myWeek=array("Today","Yesterday");

for($i=2;$i<7;$i++){
	array_push($myWeek,date('l',strtotime('-'.$i.' day')));
}

Open in new window


function getDayName($myDate,$myWeek){
	global $myWeek; // globalise the $myWeek array
	if($myDate> strtotime('-7 day')){
		return $myWeek[(time()-$myDate)/(60*60*24)];
	}
	else{
		return date('d/m/Y',$myDate); // else just return the date as a normal date string
	}
}

Open in new window


Then you just need to pass your date to the function.
If it is already in timestamp format then:
 
$dayName = getDayName($yourdatetime);

Open in new window


Otherwise convert it to a timestamp:
$dayName = getDayName(strtotime($yourdatetime));

Open in new window

0
Comment
Author:Gary
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
0 Comments

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Join & Write a Comment

Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month