Solved

date format

Posted on 2011-02-18
16
276 Views
Last Modified: 2012-05-11
I am printing dates from a database as below:

$row_rs_tax_diary['date']

Currently this prints as the following date format:

11/03/19 - i.e. Year, Month, Date

I would like it to be converted to print as

Sat 19th March

How can I do this?
0
Comment
Question by:petewinter
  • 5
  • 5
  • 4
  • +2
16 Comments
 
LVL 19

Expert Comment

by:Bardobrave
Comment Utility
0
 
LVL 36

Expert Comment

by:Loganathan Natarajan
Comment Utility
try like this,

$date = $row_rs_tax_diary['date'];

$formatted = date('M. d, Y', strtotime($date));      
0
 

Author Comment

by:petewinter
Comment Utility
logudotcom - Thanks but the result was incorrect.

The result was

Nov. 03, 2019

I want it to be

Sat 19th March
0
 
LVL 36

Expert Comment

by:Loganathan Natarajan
Comment Utility
try this,
<?php
$date = '11/03/19';//$row_rs_tax_diary['date']

$formatted = date('D jS F', strtotime($date));      
// Sat 19th March

echo $formatted;

?>

Open in new window

0
 
LVL 36

Expert Comment

by:Loganathan Natarajan
Comment Utility
i think you need pass the correct date format ...so that it displays well
0
 
LVL 36

Accepted Solution

by:
Loganathan Natarajan earned 275 total points
Comment Utility
this shows what you want,


$date = '2011/03/19';//$row_rs_tax_diary['date']


$formatted = date('D jS F', strtotime($date));      
// Sat 19th March

echo $formatted;
0
 
LVL 4

Expert Comment

by:andreizz
Comment Utility
Use this string instead of:
$row_rs_tax_diary['date'] - this must be a timestamp

date("D jS F",$row_rs_tax_diary['date']);
0
 
LVL 36

Expert Comment

by:Loganathan Natarajan
Comment Utility
you need to pass the year as "yyyy-mm-dd" ... you will get exact result
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:petewinter
Comment Utility
logudotcom - Thanks, but the result of $row_rs_tax_diary['date'] is 11/03/19 not 2011/03/19 as you have used. This makes the result Sun 3rd November not Sat 19th March.

How can I fix this?
0
 
LVL 4

Expert Comment

by:andreizz
Comment Utility
since this is the 21 century and will be for a long time, do something like this:

$new_date="20"+$row_rs_tax_diary['date'];

then you will have the format used by log
0
 
LVL 4

Assisted Solution

by:andreizz
andreizz earned 150 total points
Comment Utility
sorry i used + from javascript :P
this:


$date = "20".$row_rs_tax_diary['date'];
echo date('D jS F', strtotime($date));      
// Sat 19th March
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 75 total points
Comment Utility
Please read this article.  It will tell you most of what you need to know about using DATETIME fields in PHP and MySQL:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

For the requested format, Sat 19th March the date() pattern would be something like date('D jS F');
http://us2.php.net/manual/en/function.date.php

Best of luck with your project, ~Ray
0
 

Author Comment

by:petewinter
Comment Utility
andreizz - Thanks I understand this the simplest solution and will not cause a problem for 89 years which I'm sure will be more than enough for my website life time, but if we were going to do this without any possible future bugs how would li be done?
0
 
LVL 4

Assisted Solution

by:andreizz
andreizz earned 150 total points
Comment Utility
$date='11/03/19';
	$a=date("Y");
	$date=$a[0].$a[1].$date;
	echo date('D jS F', strtotime($date)); 

Open in new window


There you go :) this will work even when we will travel to other planets :D
0
 

Author Closing Comment

by:petewinter
Comment Utility
Many thanks all.

21st Century here we come!!! :)
0
 

Author Comment

by:petewinter
Comment Utility
or should I say 22nd Century here we come! :)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
The viewer will learn how to dynamically set the form action using jQuery.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now