This question already has answers here:
How can I compare two dates in PHP?
(13 answers)
Closed 10 years ago.
I am newbe to php. I have small problem to compare dates in php.
I have 4 dates named date1, date2, start_date and end_date in the format yyyy-mm-dd.
i need the functionality like this:
if((date1>=start_date) && (date2<=end_date)){
}
please help me to solve this.
use strtotime to get the timestamps of your dates - this can be easily compared to each other like you already trying it.
If you have PHP 5.3, this is easily handled by the DateTime and DateDiff classes. If you have not, it is much harder, but look at the 'user contributed' section on DateDiff in the online PHP manual.
Related
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 4 years ago.
Here are 3 examples of date and time strings I have
08-01-2019_03-00-34pm
22-01-2019_02-05-06pm
30-01-2019_11-17-07am
I would like to convert these to Unix Time Stamps
For example I have tried this on the first one..
echo (strtotime("08-01-2019"));
This half works, as it gives me a timestamp of 1546905600 which translates back to the correct date, but obviously makes the time 12:00:00am by default. So I'm stuck with how to deal with the time bit
I understand there is a str_replace() function so as long as I know what format I need to make the time bit, I guess I could use that?
Use DateTime::createFromFormat():
$date = DateTime::createFromFormat('d-m-Y_h-i-sa', '08-01-2019_03-00-34pm');
You can then use it to get the timestamp
$date->getTimestamp(); // 1546977634
This question already has answers here:
Calculate age based on date of birth
(11 answers)
Closed 8 years ago.
I want to store a user's date of birth, publish it and show the age. I know how to store the date of birth, but I do not know how I could echo the age afterwards.
I want it like:
username - 06/02/1992 (22 years)
How do I do this?
Essentially you just need to use the date() function to format it.
This answer will help if you're using MySQL and will also describe the basics of formatting dates.
Convert from MySQL datetime to another format with PHP
This question already has answers here:
How to compare two dates in php [duplicate]
(16 answers)
Closed 9 years ago.
How to compare two dates in php. I got an array of dates from the database and I want it to compare with todays date.
I have something like this.
$d['dd'] = 10;
$d['mm'] = 12;
$d['yy'] = 13;
Now I have done Something like this:
$date = $d['yy'].'-'.$d['mm'].'-'.$d['dd'];
$date_diff=strtotime(date("y-m-d"))-strtotime($date);
if($date_diff>0){
//this date is gone.
}
// I want to show this day will go in 5 days. To do this I have following.
if((strtotime(date("y-m-d", strtotime(date("y-m-d", strtotime(date('y-m-d'))) . "+ 5 days")))-$date_diff)>0){
// This day will go in 5 days time.
}
Now my question is what is the best way to do this or how can I do it in a better(more reliable) way. Any suggestions will be helpful.
Thanks in advance.
When doing date comparision you should do it directly in the database (if possible), as mysql knows about timezones, DST-stuff, leap years etc. PHP DateTime has a history of problems, and if you can get the database to do this I would recommend this.
Save your dates as DateTime and use the built in mysql date-functions: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP mysql insert date format
I'm trying to insert a date in to my mysql datebase. The problem is that when I for example try to save 2013-01-18 it saves it like this: 000-00-00.
$date = date("Y-m-d");
mysql_query("insert into events values('','$event','$notes','$date')");
What should I do to fix this?
It's better to convert it to Unix timestamp first, and then save the timestamp. It's a lot easier to sort etc.
As for your question: if you really want to save it that way, your column should be a 'varchar'.
Also, mysql_query is deprecated. Don't use that anymore, it won't be supported for long anymore.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to use DATE FORMAT in my query?
I have a date from form post as 09-23-2012 and in my sql database i save date with datetime format like 2012-09-23 23:11:13
I want to create a query to compare these two date.
These two date should equal.
Try DATE_FORMAT:
WHERE DATE_FORMAT(the_field, '%Y-%m-%d') = CAST(:yourInput AS DATE)
Or something similar