I was wondering if PHP has a built in function to return the relevance of a date. For example:
$timestamp = time();
$other = time()+86400;
echo rel_date($timestamp); //prints Today
echo rel_date($other); //prints Tomorrow
I know it's pretty simple functionality to build but I don't want to be reinventing the wheel!
As far as I know there is no built in function that has a format option which ouputs "Today", "Yesterday", "Tomorrow", etc..
I've made them myself because of this anyway. It seems you'll need to build it yourself after all, sorry :(
Related
I've started using PHP Carbon for my application since it seems so much easier than using and manipulating date/time with the DateTime class. What I want to do is check if the chosen date ($chosen_date) is greater than another date ($whitelist_date). I have tried this in the code below:
$chosen_date = new Carbon($chosen_date);
$whitelist_date = Carbon::now('Europe/London');
$whitelist_date->addMinutes(10);
echo "Chosen date must be after this date: ".$whitelist_date ."</br>";
echo "Chosen Date: ".$chosen_date ."</br>";
if ($chosen_date->gt($whitelist_date)) {
echo "proceed";
} else {
echo "dont proceed";
}
The original $chosen_date value comes from POST data. Here is the output I get:
Chosen date must be after this date: 2015-09-22 21:21:57
Chosen Date: 2015-09-22 21:01:00
proceed
Clearly the chosen date is not greater than the whitelist date but still the if statement returns true and echo's "proceed". I have been over the code over and over but I can't see where I have gone wrong.
It Might be, the time zones are not the same, so try this
$chosen_date = new Carbon($chosen_date, 'Europe/London');
$whitelist_date = Carbon::now('Europe/London');
$whitelist_date->addMinutes(10);
Remember you can always construct the instance and set the timezone for it:
$date = new Carbon();
$date->setTimezone('Europe/London');
$whitelist_date = $date->now();
Any tips on how I can manage data for users with different timezones?
You can create different objects with different Time zones. Try this and play with the results.
$london_date = new Carbon($chosen_date_from_london, 'Europe/London');
$colombia_date = new Carbon($chosen_date_from_colombia, 'Bogota/America');
Let's say you compare them:
$are_different = $london_date->gt($colombia_date);
var_dump($are_different); //FALSE
Nope, they're not different, although they're different times when you stare at the clock and in different parts of the world, they're still in the same Present Moment, the NOW.
There you go, just crate different objects or instances of Carbon(), and set different time zones using $instance->setTimeZone(TimeZone);
Or try using the following one:
if ($chosen_date->gte($whitelist_date))
In My SQL Database I have a Timestamp Column with values like this one representing the Date of the last edit:
2015-01-17 08:55:34.000000
I want to compare the Date with the current date and when is the same day I want to echo Today and otherwise I want to Display the Date of the last edit:
$timefromdb = '2015-01-17 08:55:34.000000'
$edit = strtotime($timefromdb);
if($edit > $_SERVER['REQUEST_TIME']){echo "Today";}
else{
echo strftime("on %A, the %d %B %Y", $edit);
}
echo " at ".date('h:i',$edit)
It always Displays 01/01/1970. There must be a Problem with strtotime. I did a bit of research and it seems like my Timestamp Format isn't a valid one: http://php.net/manual/en/datetime.formats.php
Around the web are a lot of Questions about converting Timestamps but I just can't find the right one: I also got a bit confused by all the functions to convert date stuff.
So can someone Tell me how to get a valid Timestamp for using it in strftime and to compare it to the REQUEST_TIME.
Thanks in Advance!
UPDATE: As Always: The Problem sits in Front of the PC. I declared the Variable but never assgined the Timestamp to it :)
Chop off the .000000 from the date as it makes the date a format strtotime() cannot work with. There's several ways to do this. A simple substr is one of them.
$timefromdb = substr('2015-01-17 08:55:34.000000', 0, -7);
I'm not exactly understood you, but
try
1. compare gettype( $edit ) and gettype($_SERVER['REQUEST_TIME'])
2. not sure what $timefromdb will be more then $_SERVER['REQUEST_TIME'], because IMHO when user edited data, time of it action will me less then current time.
Hi everyone im stuck with this: I want to erase records on a database that are in the past. I just want to get into account Month and Day. For example, if the database record is (this is how is formatted on the DB) "Apr 5 2013" i need to compare it with today's date "Apr 6 2013". In this case, this record gets deleted. I´ve seen examples using UNIX timestamp, but none using that format using the date('M j Y'). Thanks!
The better way to store dates in databases is using DATE or DATETIME format. SQL allows you to get all informations you want from those types. But a request like this should work.
You could do it eventually with a regular expression, but it would be very heavy...
But you can do it in PHP, getting all results, then browsing them and comparing dates using something like strtotime, and then deleting every ID. Still very heavy but easier to implement than within a SQL request. But it still would be better if you could change the SQL architecture.
The approach I would try would be to convert the english text string into a unix timestamp using strtotime() then compare the new timestamp to the current date. Here is some php pseudo-code that describes the approach.
$threshold_to_deletion = numbers of days old a record is allowed to be;
$is_it_yesterday = 0;
$current_date = new DateTime();
$current_date->getTimestamp();
if ( !($is_it_yesterday = strtotime($str_from_db)) === false ) {
$date_interval = date_diff($is_it_yesterday, $current_date);
if( $date_interval >= $threshold_to_deletion ) {
Do stuff here to delete
}
} else {
echo "DB string is not a valid date: $str_from_db";
}
Please note it is not finished or tested, it's just there to loosely describe how it would be done. I hope this helps.
Tim Dumas
www.mpact-media.com
I am trying to put a readable time and date as part of a file name (in php). I am having all kinds of trouble with this and was hoping someone could help. I have tried several different recommendations that I have read around the internet (plus I read the manual) but I really haven't gotten anything to work right. Right now I have this:
$Time=strtotime("now");
$date=DateTime::createFromFormat('m/d/Y H:i:s', '7/24/2012 14:40:30');
$date_readable=$date->$Timestamp();
At that point I then add $date_readable to a file name. It compiles and runs but it doesn't format the date at all. It still gives it as a timestamp.
Any suggestions on how to make this work?
you can do it with simple date function for example
$time = strtotime("now");
$formatDate = date('F jS, Y h:i:s A', $time);
echo $formatDate;
this will print something like
July 25th, 2012 1:02:29 am
DateTime class is more powerful then using simple date function, as DateTime class offers powerful API's plus it is object oriented. however for simple date conversions i would stick to php's date function. as that could do my purpose.
for more formatting option have a look at this link http://www.php.net/manual/en/function.date.php#refsect1-function.date-parameters
I would like to show the number of days missing for a specific date. In other words, I want to display something like:
X days to go for the great event
using PHP and the server time.
<?php
$event_date = '2010-01-01 00:00:00';
$event_time = strtotime($event_date);
$diff = $event_time - time();
echo floor($diff/(24*60*60)).' days to go for the great event';
?>
Note: I'm totally side stepping any timezone considerations, so, be sure you read up on timezone issues associated with using the PHP datetime functions.
jakemcgraw's answer would have my vote, had I 15 rep. :)
You might want to use mktime() instead of strtotime(), though.