<

[Last Call] Learn how to a build a cloud-first strategyRegister Now

x

Display your dates as Today, Yesterday etc in PHP

Published on
3,904 Points
904 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
0 Comments

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Join & Write a Comment

The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month18 days, 1 hour left to enroll

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month