---
title: "PHP Mysql - date and time"
type: "forum-thread"
url: "https://www.webforum.nu/amne/php/184121-php-mysql-date-and-time"
topic: "PHP"
topic_url: "https://www.webforum.nu/amne/php"
author: "webb"
published: "2010-07-18T09:34:29.000Z"
updated: "2010-07-18T13:43:13.000Z"
replies: 9
views: 678
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/php/184121-php-mysql-date-and-time"
---

# PHP Mysql - date and time

## #1 — webb, 2010-07-18T09:34Z

Hej,
Jag skriver ut senaste nyhet från mysql med följande kod:
select \* FROM nyhet ORDER BY date

Jag använder: date("Y-m-d H:i:s")

utskrift på datumet ser ut så här:
2010-06-15 19:17:37

Ibland behöver jag en tidigare nyhet strax innan den senaste nyheten tex:
2010-06-15 12:17:20 om senaste datumet är tex: 2010-06-15 19:17:37

Är det någon som kan hjälpa mig?

Tack i för hand!

Hälsningar
webb

Permalänk: https://www.webforum.nu/p/184121

## #2 — noshornin, 2010-07-18T12:22Z

```
SELECT * FROM nyhet WHERE date < '2010-06-15 19:17:37' ORDER BY date LIMIT 1
```

Borde funka, jag har inte testat det!

Permalänk: https://www.webforum.nu/p/2243405

## #3 — webb, 2010-07-18T12:45Z

Tack  för svaret, det fungerar inte. Jag vill skriva ut föregående nyhet på webbläsare utan att behöva veta om datum/tid. Genom att använda tex. date 
select \* FROM nyhet ORDER BY date DESC LIMIT 1 =\> men med föregående datum/tid

Permalänk: https://www.webforum.nu/p/2243411

## #4 — Quid, 2010-07-18T12:52Z

```
SELECT * FROM nyhet ORDER BY date DESC LIMIT 1, 1
```

Permalänk: https://www.webforum.nu/p/2243412

## #5 — GunnarD, 2010-07-18T12:58Z

Titta på vad LIMIT kan göra:

Denna kod ger tillbaka senaste nyheten:

```
SELECT * FROM nyhet ORDER BY date LIMIT 1
```

Denna kod ger tillbaka näst senaste nyheten:

```
SELECT * FROM nyhet ORDER BY date LIMIT 1.1
```

> **http://dev.mysql.com/doc/refman/5.0/en/select.html skrev:**
>
> The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants (except when using prepared statements).
> 
> With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):
> 
> SELECT \* FROM tbl LIMIT 5,10;  # Retrieve rows 6-15
> 
> To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter. This statement retrieves all rows from the 96th row to the last:
> 
> SELECT \* FROM tbl LIMIT 95,18446744073709551615;
> 
> With one argument, the value specifies the number of rows to return from the beginning of the result set:
> 
> SELECT \* FROM tbl LIMIT 5;     # Retrieve first 5 rows
> 
> In other words, LIMIT row_count is equivalent to LIMIT 0, row_count.

/red: quid han före :)

Permalänk: https://www.webforum.nu/p/2243414

## #6 — webb, 2010-07-18T13:11Z

Tack så mycket för hjälpen, det fungerar som jag önskade!

Tack alla!

Hälsningar
Webb

Permalänk: https://www.webforum.nu/p/2243418

## #7 — Quid, 2010-07-18T13:13Z

Markera gärna ett av inläggen som en lösning.

Permalänk: https://www.webforum.nu/p/2243420

## #8 — webb, 2010-07-18T13:30Z

EN fråga till!

Om jag vill gå 2 steg tillbaka (näst näst senaste nyheten)? 
kan jag använda 2, 1?

Permalänk: https://www.webforum.nu/p/2243421

## #9 — GunnarD, 2010-07-18T13:41Z

Ja, 0.1 returnerar senaste, 1.1 näst senast, 2.1 tredje senaste ....

Permalänk: https://www.webforum.nu/p/2243422

## #10 — webb, 2010-07-18T13:43Z

Tack så mycket!! :)

Permalänk: https://www.webforum.nu/p/2243423

---

Tråden på webben: https://www.webforum.nu/amne/php/184121-php-mysql-date-and-time
