I am trying to get current time and date in order to echo it out on my website. I have the follow snippet:
$date_of_msg = date("Y-m-d");
$time_of_msg = time();
When I echo $time_of_msg I get 00:00:00. I have tried to edit my code based on this solution here but which this approach, when I echo the variable, I get 838:59:59. I simply want the current time to be displayed in 24 hour format.
In addition to this, I currently have the date formatted to (Y-m-d), which is great because it works. I am trying to format it so that it displays day, number, year, i.e. today is 20th Feb, so I want the date to display Feb 20, 2016. I have tried the following based on documentation (see here)
$date_of_msg = date("F j, Y")
But again, the date displays nothing. Am I missing something?
If you need the current time in the 24h format just use
$time_of_msg = date("H:i");
The date part seems correct that way, you must be doing something wrong while displaying it.
time() (unless you override it in some weird fashion) gives you a timestamp, i.e. the amount of seconds which have passed since 1970-01-01 until now. date(), however, gives you a string representation of a date, which may or may not include the minutes and seconds - depending on how you choose to format it.
So, if you want to display the time and date to a user, you should probably go for something like
$date_of_msg = date("F j, Y H:i:s")
The documentation on date() gives you an excellent description of available options.
time() returns a UNIX timestamp while date() format a timestamp. Your call date("Y-m-d") means the same as date("Y-m-d", time()).
Even though the function is called date(), it can also format time. You just have to use the correct placeholders. E.g. date("H:i:s") would give you a 24h-time like 17:43:23.
This code
<?php
echo date("Y-m-d").PHP_EOL;
echo time().PHP_EOL;
echo date("F j, Y").PHP_EOL;
Returns this result, as expected
2016-02-20
1455927480
February 20, 2016
So what are you doing that you are not actually telling us
Related
I am trying to get a simple line of text to appear if todays date is after another date.
I can either get it to appear on all pages or none, but I am unable to get it to display based on whether the challenge start date is before or after todays date. I believe it could be a date format issue, but everything I have tried has fallen short.
Here is my code:
Get todays date
$date_now = new dateTime();
Challenge start date
$challengeStartDate = date('dS F Y', strtotime($this->item->start_date));
echo '<!--' . strtotime('1970/1/1 00:00:00 +' . $validity) . '-->';
New text line
if ($challengeStartDate > $date_now) echo "New Text";
date() returns a string. With $challengeStartDate > $date_now it's like comparing if one string is bigger than the other (not sure if your dateTime handles that).
Your approach is otherwise fine. Just use timestamps to compare. time() gets you the time as a Unix timestamp:
$now = time();
if ($now > strtotime($this->item->start_date)) {
// do your thing
}
Something like this is more what you need. Try it out.
I had the very same problem some time ago.
All you need to do is store your local time in a database so it would be saved statically.
Because in your example, both $challengeStartDate and $date_now will change and update simultaneously and you wiill always get the current pc time!
Try storing it in a table or idk maybe sessions would help too.
So I am trying to get the Last Modified date of a file in PHP, I am using this:
$file = "my_file.txt";
$date = date("F j, Y H:i:s.", filemtime($file));
Now the problem is when I echo it, I do not get it in my timezone.
It comes up in UTC time, but I am in UTC-4 timezone.
(e1:) How do I get it to show my timezone, or even (e2:) display it in the timezone for whoever is viewing the webpage?
Example 1: Lets say I update the file at 1pm (UTC-4). Anyone looking at the webpage will see it displayed as 1pm (UTC-4) regardless of where they are located.
Example 2: Say I update the file at 1pm (UTC-4), and someone from the timezone UTC is looking at my webpage; they will see the time 5pm (UTC), because that's the time I uploaded it in their timezone.
Get the server time from PHP and store it in an HTML element. (There are other ways to pass the data, but this one is pretty easy to understand.) Here, I just used the time() function to grab the current time. Your filemtime method should return a timestamp if you omit the formatting options.
Then, grab the timestamp from the <div> with JS. Since the JS want milliseconds, multiply it by 1000. Then use toLocalString() to offset it to the user's local time.
<div id="timeholder" data-time="<?php echo json_encode(time()); ?>"></div>
<script type="text/javascript">
var date = new Date(document.getElementById('timeholder').dataset.time * 1000).toLocaleString();
console.log(date);
</script>
For Example 1:
Thanks to #thingEvery for doing the math on how much I needed to subtract from the current timestamp.
$filetime = date("U", filemtime($pfile));
$hoursToSubtract = 4;
$timeToSubtract = ($hoursToSubtract * 60 * 60);
$mytime = $filetime - $timeToSubtract;
# I needed to print it in two different formats,
# that's why I split it up
$time1 = date("F j, Y", $mytime);
$time2 = date("g:i A [e-4]", $mytime);
I'm having some problems displaying the TIME Only (H:i) in a row.
<td headers="qf_start_date" nowrap="nowrap"><?php echo date_format($course->start_date, '%H:%i'); ?></td>
This is the code, and as you can see, i want to see, under the start_date table in phpmyadmin, only Hours and minutes, nothing else. In the start_date query I want to have only, at the same time Y-m-d but i want to see in a page, only the hours and minutes...
I tried using date_format but it doesn't work...
What can i do?
I tried to include JDate but i think that i used a wrong sintax so i see blank page...
Thanks
To be more clear, visit: http://new.ivao.ch/index.php?option=com_seminarman&view=category&cid=4:rfe-geneva-departure&Itemid=227 and the table that i'm working in STD. It's blank...dunno why..
Thanks in advance
please followw at below :
<?php
$date = "6.1.2009 13:00+01:00";
print_r(date_parse_from_format("j.n.Y H:iP", $date));
?>
or
<?php
$date = date_create('2000-01-01 12:15:25');
echo date_format($date, 'H:i:s');
?>
or
<?php
$valid_date = date( 'm/d/y g:i A', strtotime($date));
?>
or
<td headers="qf_start_date" nowrap="nowrap">
<?php
$valid_date = date( '%H:%i', strtotime($course->start_date));
echo $valid_date;
?>
Since it looks like you're working with Joomla, you can use the JDate class to help you.
If your $course->start_date is a valid date string (can be converted with strtotime, you may do the following):
// May fix class JDate not found errors. At the top of your script.
jimport( 'joomla.utilities.date' );
// ...
$date = new JDate($course->start_date)
echo $date->format('H:i');
Alternatively, (from the comments) it seems like your date string is not formatted as a UNIX timestamp. If you're not using Joomla, or do not want to use JDate you may use:
echo date('H:i', strtotime($course->start_date));
What's happening here is, for most date processing functions in PHP, you must pass it a valid UNIX timestamp.
A UNIX timestamp is measurement of the number of seconds since the Unix Epoch on January 1st, 1970 at UTC.
You can convert most date strings into one (including mysql DATETIME columns by passing it into php's strtotime() function.
From there, you pass it into date (or your desired date formatting function) with a desired format. Hope this helps!
Okay so I have an array of results from a table, we have a start and end time we are retrieving. the start/end time would look something like this:
1345497551
Now I'm trying to convert this to a real time, for instance 1345497551 might become 2012/05/09 17:23:12 or something. I've found a few things but none seem to work correctly. one solution I tried, according to what someone was saying on another question on here, was
$createdate = date('H:i:s',$numberofsecs);
where $numberofsecs was the time pulled in from the array. but this only ever outputs 17:00:00 repeatedly for every time we had available for testing.
How can I go about making this work correctly?
Assuming that that's a standard unix timestamp string (seconds since midnight 1/1/1970), then you should be able to use date as you mentioned, but just modify the format string:
echo date('Y/m/d H:i:s', $numberofsecs);
The example you mention where you were always getting 17:00:00 could have been because your test cases were all only datestamps, encoded as timestamps, and having an offset from GMT . . .
I have tried below code:
$ts = 1345497551;
$date = new DateTime("#$ts");
echo $date->format('U = Y-m-d H:i:s');
output : 1345497551 = 2012-08-20 21:19:11
I have been looking online for this answer and have come up empty...I am extremely tired so I thought I would give this a go....
I have a variable that has a date from a textbox
$effectiveDate=$_REQUEST['effectiveDate'];
What I am trying to do is take this date and add the current time
date('Y-m-d H:i:s', strtotime($effectiveDate))
When I echo this out I get 1969-12-31 19:00:00
Is this possible? Can someone point me in the right direction?
I found a solution to my problem....
$currentDate = date("Y-m-d");
$currentTime = date("H:i:s");
$currentDate = date("Y-m-d H:i:s", strtotime($currentDate . $currentTime));
echo $currentDate;
This takes a date from variable in one format and takes the date from another variable in another format and puts them together :)
Thanks everyone for their time.....
DateTime::createFromFormat
would also work but only if you have PHP 5.3 or higher...(I think)
The effectiveDate string is not in a format that strtotime recognizes, so strtotime returns false which is interpreted as 0 which causes the date to be displayed as January 1, 1970 at 00:00:00, minus your time zone offset.
The result you see is caused by the entered date not being in a format recognised by strtotime. The most likely case I can think of without knowing the format you used is that you used the US order of putting the month and day the wrong way around - this confuses strtotime, because if it accepts both then it can't distinguish February 3rd and March 2nd, so it has to reject US-formatted dates.
The most reliable format for strtotime is YYYY-MM-DD HH:ii:ss, as it is unambigous.
The date is just a timestamp, it is not object-oriented and i don't like it.
You can use the DateTime object.
The object-oriented best way is:
$effectiveDate=$_REQUEST['effectiveDate'];
// here you must pass the original format to pass your original string to a DateTimeObject
$dateTimeObject = DateTime::createFromFormat('Y-m-d H:i:s', $effectiveDate);
// here you must pass the desired format
echo $dateTimeObject->format('Y-m-d H:i:s');