Jump to content

Recommended Posts

I am in need of assistance. I'm learning and advancing in php, although I've come across a problem.

 

I'm working on a project and I want to to show the time of something and then another part to show just the date. In my database I have a field called date_played and it has a format like this:

2012-11-23 23:27:06

 

What I want to do is have php look for the 2012-11-23 for the date and then 23:27:06 for the time and then go even further with the date feature have it echo the time like 11:27 PM ( I think I know that part).

 

Can you please help? Your help would be greatly appreciated!

Link to comment
https://forums.phpfreaks.com/topic/271098-displaying-portion-of-field-from-mysql/
Share on other sites

date function

 

eg

 

<?php
$db_date = "2012-11-23 23:27:06";   // date from DB

echo date("jS F Y", strtotime($db_date)) . '<br />';	   // 23rd November 2012
echo date("d M Y", strtotime($db_date)) . '<br />';	    // 23 Nov 2012

echo date("g:i A", strtotime($db_date)) . '<br />';	    // 11:27 PM

?>

And i've tried the date

 

$sql = "SELECT * FROM historylist WHERE `songtype` = 'S' AND `date_played` = 'date(Y-m-d)";

and no go. I had something to where I got only the time and that worked, but I can't get the SQL to get songs by the current date.

date('Y-m-d') is a PHP function. Use

 

$sql = "SELECT * FROM historylist WHERE `songtype` = 'S' AND `date_played` = CURDATE()";

 

Correction - as the dates in DB contain time elements you need

 


$sql = "SELECT * FROM historylist WHERE `songtype` = 'S' AND DATE(date_played) = CURDATE()";

 

The MySQL DATE() function gets just the date part from the date field

Edited by Barand

Given your example

 

$searchDate = date('Y-m-d', $_GET['date']);	 // accepts date=23 nov 2012 and other formats also and ensures correct format

$sql = "SELECT * FROM historylist WHERE `songtype` = 'S' AND DATE(date_played) = '$searchDate' ";

Woah! Hey thanks for your help!

 

I got another question on pagination.. http://www.lite945.com/iplaylist/playlist.html take a look at the bottom.. See how that is. I'm trying to figure out how to get that. I know how to get the results from the database, for what I'm doing, but pagination is a whole nother thing haha.. Any help is greatly appreciated. Thanks again!

Use a query with a "LIMIT r, n" clause at the end

 

"r" is row number to start from

"n" is the number of rows to retieve

 

If "p" is the current page (starting at 1) then r = (p-1) * n

 

At the bottom (and/or top) of the page you have a list of page number each (except current) have a link to that page (?page=n)

 

I usually show first and last with a couple each side of the current page eg

 

1 ..... 10 11 12 13 14 ...... 20

That's what I said.

 

// first 20 rows - page 1
$sql = "SELECT * FROM historylist WHERE `songtype` = 'S' AND DATE(date_played) = '$searchDate' LIMIT 0,20";
// page 2
$sql = "SELECT * FROM historylist WHERE `songtype` = 'S' AND DATE(date_played) = '$searchDate' LIMIT 20,20";

//etc

I was messing around with the date thing and got it going to where I could do -3 days, etc.. However, the problem is I can keep going to 3 more days, and I'm not stoo sure how to do that part. Can I do something like $mindate or something?

http://www.mradio105fm.com/iplaylist/mediabase/history.php

 

On there, I have a drop down that says select date, I can go back to three days, and when I click on previous days, I can continue going back in days, but I want it so after the 3 days they can't go anymore. Like if you keep playing with the drop down, you'll notice what it's doing.

And back to the pagination on that link on page one that was playlist.html

 

You see numbers like this at the bottom:

 

1-20 21-40 , etc and since it's no net or if net=1 it bolds that.. That's the pagination i'm trying to do. Do I count my artists in my songlist table or how am I doing that? It's new to me

Excerpt of how that pagination on that page is.. If you click the links, you can see how it works!

oh and I believe it's doing the date selected. How should I echo to have it do the today's date? That might be the problem with that!

Edited by mfoland
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.