Jump to content

using php and date?


onenonly

Recommended Posts

how does php know of comparing dates not numbers with greater than or less than symbol?

 

I really dont know how to code date with php and mysql.

 

What the best way to input a record into the database but post it two days later?

 

 

should i put in current time plus 48 hours later into the database

but how do i increment the time by 48 hours?

 

Link to comment
Share on other sites

<?php

$date = date("Y-m-d");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +12 month");
$date = date("Y-m-d",$date);
echo $date;

//Other examples
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +2 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 month");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +30 days");
?>

 

Change information as required

Link to comment
Share on other sites

how does php know of comparing dates not numbers with greater than or less than symbol?

When a string or a number is formatted with fields ordered left to right, MSD (Most Sugnificant digit) to LSD (Least Sugnificant Digit), which for a date would be YYYY-MM-DD (which is why database dates are formatted that way), they can be directly compared and sorted.

 

should i put in current time plus 48 hours later into the database

but how do i increment the time by 48 hours?

You should always put the current date/time into the database (so you both know when it was inserted and in case you need to change exactly how far into the future/past it gets selected) and then form your query to select that data/time when you want it.

Link to comment
Share on other sites

s

how does php know of comparing dates not numbers with greater than or less than symbol?

When a string or a number is formatted with fields ordered left to right, MSD (Most Sugnificant digit) to LSD (Least Sugnificant Digit), which for a date would be YYYY-MM-DD (which is why database dates are formatted that way), they can be directly compared and sorted.

 

should i put in current time plus 48 hours later into the database

but how do i increment the time by 48 hours?

You should always put the current date/time into the database (so you both know when it was inserted and in case you need to change exactly how far into the future/past it gets selected) and then form your query to select that data/time when you want it.

 

 

 

so i should put the current time into the database then

parse the code the date entered into the database plus 48 hours?

 

if i have the database auto enter the number into the database with the current time

how do I break up the datetime to parse it by 48 hours?

Link to comment
Share on other sites

Mysql has a number of date/time functions that you can use in a query to do just about anything. For selecting records some amount of time into the past/future you would use either the DATE_ADD or DATE_SUB functions -

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-add

 

Do you have a specific example of what you are entering into the database and an example of how you want to retrieve it? Are you entering current dates and want to display them until they are 48 hours old (i.e. news stories) or are you entering future dates and want to display them when you are within 48 hours of the date (i.e. calendar events?) or some mix?

Link to comment
Share on other sites

Mysql has a number of date/time functions that you can use in a query to do just about anything. For selecting records some amount of time into the past/future you would use either the DATE_ADD or DATE_SUB functions -

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-add

 

Do you have a specific example of what you are entering into the database and an example of how you want to retrieve it? Are you entering current dates and want to display them until they are 48 hours old (i.e. news stories) or are you entering future dates and want to display them when you are within 48 hours of the date (i.e. calendar events?) or some mix?

 

All i want is when i insert something into the database

I want it to be displayed 48 hours later

what is the best way of doing this?

 

$a = (current + 48 hours)

$b = (current time)

 

should insert the $a into the database

and when $b < $a  display it

 

or

 

should I insert the $bb into the database

and display the code only if ($bb + 48 hours) > $b

 

also

should I parse date into mysql code

or keep it php date so its eaier to handle?

Link to comment
Share on other sites

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.