strtotime not working with TIME? - php

My mysql column has this datetime value, 2011-04-11 11:00:00 when I am applying strtotime then its returning date less than today,whereas it should be greater than today.
also when I am trying this strtotime(date('d/m/Y h:i A')); code, its returning wrong values. Is there any problem with giving TIME in strtotime?
Basically I want to do, is to compare my mysql column date with today's date, if its in future then show "Upcoming" else show nothing?
Please help and advise, what should I do?
Edited code
$_startdatetime = $rs['startdatetime'];
$_isUpcoming = false;
if(!empty($_startdatetime)){
$TEMP_strtime = strtotime($_startdatetime);
$TEMP_strtime_today = strtotime(date('d/m/Y h:i A'));
if($TEMP_strtime_today < $TEMP_strtime){
$_isUpcoming = true;
$_startdatetime = date('l, d F, Y h:i A' ,$TEMP_strtime);
}
}
And the value in $rs['startdatetime'] is 2011-04-11 11:00:00. And with this value I am getting following output.
$TEMP_strtime - 1302519600
$TEMP_strtime_today - 1314908160
$_startdatetime - 2011-04-11 11:00:00
$_startdatetime its value is not formatted as the upcoming condition is false, so returning as is mysql value.

d/m/Y h:i A is irreversible (with strtotime) format, use standard formats or use time() as recommended by Joel & Rocket
PROBLEM
<?php
echo $today = date('d/m/Y h:i A');
echo '<br />';
echo $time = strtotime($today);
echo '<br />';
echo date('d/m/Y h:i A', $time);
OUTPUT
SOLUTION
<?php
$today = strtotime(date('m/d/Y h:i:s')); <- use appropriate format
// OR
$today = time(); #credit to Joel & Rocket

Related

How to compare todays date time with given date time, date time format is like date('d M Y - H:i A')

i want to compare current date & time with database date & time, the condition is
if($current_date > $given date) {
echo "deactive status";
}
main problem is date format is date('d M Y - H:i a'); i want to compare this format only
You can use DateTime::createFromFormat() to create a datetime-object, which can be compared against other datetime objects. $row['date'] is the database value.
$database_time = DateTime::createFromFormat("d M Y - H:i a", $row['date']);
$today = new DateTime();
if ($today > $database_time) {
echo "Deactive status";
}
PHP.net on DateTime::createFromFormat()

Using strtotime to format date() with AM/PM

I have this line of code I wrote to create a formatted timestamp on emails from my contact us page.
It is working fine, but I'm wondering if it is written poorly and could be reduced into more efficient code? It feels wrong calling date() three times in one line. I'm not a developer by trade.
$timestamp = date('m-d-Y')." ".date('h:i A', strtotime(date('H:i:s')));
which results in: 05-28-2014 03:49 PM
Thoughts?
When you need the current timestamp, you can use,
$timestamp=date("m-d-Y h:i A");
When you need to format the timestamp you fetched from database or other means, you have to use strtotime.
$format_timestamp=date("Y-m-d H:i:s", strtotime($timestamp)); // I just convert your format to YYYY-MM-DD HH:MM:SS format.
Edit:
When you need to subtract x hours from the current time, use
$timestamp=date("m-d-Y h:i A", strtotime("-4 hour"));
Some more examples,
$timestamp=date("m-d-Y h:i A", strtotime("+2 hour")); // Adds 2 hours
$timestamp=date("m-d-Y h:i A", strtotime("+1 day")); // Adds 1 Day
You can simplify making:
$timestamp = date('m-d-Y h:i A');
or
$timestamp = gmdate("M d Y H:i:s");
For more information, see:
PHP Manual
date http://www.php.net/manual/en/function.date.php
gmdate http://www.php.net/manual/en/function.gmdate.php

php filemtime 24 hr format

I created this code to get the date a file was last touched then display it to the user in AM/PM format.
It doesn't seem to be working though. I know I'm close; what am I doing wrong?
$filename = 'test.html';
if (file_exists($filename)) {
$date = date(filemtime($filename));
clearstatcache();
}
echo "- last updated: " . date('F d Y h:i A', strtotime($date));
Output: last updated: December 31 1969 06:59 PM
Try this:
if (file_exists($filename)) {
$date = filemtime($filename);
clearstatcache();
}
echo "- last updated: " . date('F d Y h:i A', $date);
In your code, this line:
$date = date(filemtime($filename));
wouldn't work since filemtime returns a UNIX timestamp, which you are then passing as the first parameter to date(). Even if that did work, you are then converting that date back to a UNIX timestamp with strtotime(), and then back into a date string again which seems a little inefficient.
Also consider what happens if the file doesn't exist, will $date have been set elsewhere in your code?
$date = date(filemtime($filename));
That line is wrong. First argument to date() is a format string. Replace with:
$date = filemtime($filename);
Also, you don't need to perform strtotime() on a timestamp, just use as is:
echo date('F d Y h:i A', $date);

String date current date/time?

I am using $date = date("D M d, Y G:i");.
When I echo $date, it shows the correct date/time. Now I need this as an string.
I have tried string($date); but nothing happens here. And
$today = strtotime($date);
here I get weird numbers..
I need a string so I can put $today in a message.
What is the correct method for this?
The date() function already returns a string.
Doing this :
$date = date("D M d, Y G:i");
You'll have the current date in the $date variable, as a string -- no need for any additional operation.
If you like working with objects you can do this:
$date = new \DateTime('now');
echo $date->format('D M d, Y G:i');
Your $date variable is a string, there's no need for any conversion.
You can have a look at the documentation: http://ch.php.net/manual/en/function.date.php. The return value of the date() function is string.
The strange numbers you see when you call strtotime() is the Unix timestamp which represents the number of seconds elapsed since January 1 1970 00:00:00 UTC.
You're already getting a string. $date can be used like any string now.
strtotime() actually gives you the number of seconds in time like unix
$date = 'Today is '.date("D M d, Y G:i", time());
echo $date;
With regards to:
$today = strtotime($date);
Those numbers are the current timestamp (the number of seconds since January 1st 1970).
You can use this as a second parameter in the date function to change the date to whatever you want.
$newDate = date("D M d, Y G:i", $timeStamp);

How to get AM/PM from a datetime in PHP [duplicate]

This question already has answers here:
Convert from MySQL datetime to another format with PHP
(18 answers)
Closed 2 years ago.
I have a date time in a variable. My format is 08/04/2010 22:15:00. I want to display this like 10.15 PM. How to do this in PHP?
You need to convert it to a UNIX timestamp (using strtotime) and then back into the format you require using the date function.
For example:
$currentDateTime = '08/04/2010 22:15:00';
$newDateTime = date('h:i A', strtotime($currentDateTime));
$dateString = '08/04/2010 22:15:00';
$dateObject = new DateTime($dateString);
echo $dateObject->format('h:i A');
Use strtotime() to make the date a UNIX timestamp.
For output, check out the various options of date().
$timestamp = strtotime("08/04/2010 22:15:00");
date("h.i A", $timestamp);
<?php
$dateTime = new DateTime('now', new DateTimeZone('Asia/Kolkata'));
echo $dateTime->format("d/m/y H:i A");
?>
You can use this to display the date like this
22/06/15 10:46 AM
Like this:
$date = '08/04/2010 22:15:00';
echo date('h:i A', strtotime($date));
Result:
10:15 PM
More Info:
date
strtotime
for flexibility with different formats, use:
$dt = DateTime::createFromFormat('m/d/Y H:i:s', '08/04/2010 22:15:00');
echo $dt->format('g:i A')
Check the php manual for additional format options.
PHP Code:
date_default_timezone_set('Asia/Kolkata');
$currentDateTime=date('m/d/Y H:i:s');
$newDateTime = date('h:i A', strtotime($currentDateTime));
echo $newDateTime;
Output: 05:03 PM
$currentDateTime = $row['date'];
echo $newDateTime = date('l jS \of F Y h:i:s A', strtotime($currentDateTime));
Perfect answer for AM/PM live time solution
<?php echo date('h:i A', time())?>
Just simply right A
{{ date('h:i A', strtotime($varname->created_at))}}
For (PHP >= 5.2.0):
You can use DateTime class. However you might need to change your date format. Didn't try yours.
The following date format will work for sure: YYYY-MM-DD HH-MM-SS
$date = new DateTime("2010-04-08 22:15:00");
echo $date->format("g"). '.' .$date->format("i"). ' ' .$date->format("A");
//output
//10.15 PM
However, in my opinion, using . as a separator for 10.15 is not recommended because your users might be confused either this is a decimal number or time format. The most common way is to use 10:15 PM
It is quite easy. Assuming you have a field(dateposted) with the type "timestamp" in your database table already queried and you want to display it, have it formated and also have the AM/PM, all you need do is shown below.
<?php
echo date("F j, Y h:m:s A" ,strtotime($row_rshearing['dateposted']));
?>
Note: Your OUTPUT should look some what like this depending on the date posted
May 21, 2014 03:05:27 PM

Categories