How do you convert an input date like 01/16/2013 to a time format similar to 1422424719. Whats the best way to compare these two time stamp.
$timestamp = strtotime('01/16/2013');
Just compare them as you would do with any other variable. <, >, = ... are all available
Also check the PHP manual for the DateTime class. It will make handling dates and times easier.
Related
I have a column of a type "time" in mySQL database. I am displaying it in a 12 hour format on a website. Why do I need to use strtotime()?
$time = date('g:i a', strtotime( $time)) ;
I know that it works, I am just wondering why I need to use a special function to convert one type of "time" into another. Does PHP consider mySQL time column type a string?
Because the date() function accepts an integer timestamp, which denotes the number of seconds sinds Epoch. In your database, the time is stored differently (a date string, which PHP just sees as a normal string), so you need to convert it first. So, your assumptions are right.
I have two dates in the database which are from and to dates and I would like to check if today's date fits with this date range.
Is there a way we could use Cake helper to check if today's date is within these two intervals?
Thanks a lot.
You can filter in mysql query; or if you want to make a boolean value, you'll need to do it after query. It is as simple as comparing strings: $inrange = '2011-01-02 12:21:00'<$mysqldate && '2011-03-02 12:21:00'>$mysqldate;
Or if the dates are in difference format, convert to timestamp (using strtotime) and compare.
I currently have a date that's being stored in my SQL database as a VARCHAR of 255 characters. I declared this string as
//within an object...
$date = date(DATE_RFC822);
Now, later on in the coding, I realise that I need to actually compare dates with each other. My initial, very naive attempt looked a little bit like this:
if(object_1->date > object_2->date){
//do this that assumes that object_1 was created at a later date than object_2
}else{
continue;
}
While this worked fine for different times of the same day; using the code a week later began to show significant bugs.
strtotime() converts a string into unix time (an integer) which can easily be compared with other unix time values. If you are running PHP 5.2.8 or greater, you could also make use of the DateTime class which are relatively easy to compare (see this question for more info)
You can't compare dates in DATE_RFC822 format with each other. You should use Date or DateTime fields in MySQL and DateTime or Unix timestamps in PHP. It's safe to use your DATE_RFC822 string in the PHP DateTime constructor or in strtotime(). (Still, if you use Date or DateTime in MySQL you can also sort by date and search by date, etc.)
PHP DateTime objects can be compared with each other like normal PHP variables, so you can do $date1 < $date2 to determine if $date1 is before $date2.
So I've got a text input that get's date & time in this format:
09/06/2010 21:08 (meaning September)
I'm using date_parse_from_format('m/d/Y H:i', $stringtoparse); (someone tell me if I'm incorrect here)
That function returns an associative array with indexes like "year" or "day" or "hour".
The original text that was sent in needs to be treated as though it were in a certain timezone. It'll always be EST, but I'm just having troubles figuring out how to adjust for the timezone anyway.
Basically, in the end, there will be two datetimes in the database (a start time and an end time). I need to know if the current time is in between those two times. I know once I have a final timestamp I can just use:
$startdate < now() < $enddate or something like such (of course now needs to be in the format of "EST", but I need to be sure timezones are correct first. How do I do this?
You should store all dates in single timezone, we have chosen UTC for DB storage and are converting it to user's timezone on render.
You are not incorrect, however I recommend using DateTime::createFromFormat instead.
I have a mysql table field set as time type which stores data in the HH:mm:ss format. So when I list the data, it prints as, for example, 16:30:00. But I want to display hh:mm part only (not the seconds).
In case of datetime types, I can do date('H:i', '2010-03-16 16:30:00'). I mean I can retrieve any part. I wonder if there is any similar way like this for time only fields??
Please see, I can manipulate the time string to get rid of seconds in time part using str_replace, explode etc, I just wonder if there is any standard function there which I am not aware of.
If you want to do this with PHP, you'd have to get a timestamp from the time first, e.g.
echo date('H:i', strtotime('16:30:00'));
will output 16:30. Strtotime will assume the time is for the current date then.
You can let MySQL return the data in the format you want using Date_Format()
edit: as fireeyedboy pointed out there's also a TIME_FORMAT() function.
Another approach is:
SELECT UNIX_TIMESTAMP(time_field) AS tstamp FROM table;
Then you can use PHP's date() function to format it.
$time = date('H:i', $tstamp);