what is the best way to take a UTC timestamp (integer) and parse it for only the year, month, and day in PHP? Thanks
Use the PHP date() function.
<?php
$timestamp = strtotime("2011-9-12 05:48:00");
$year = date('Y', $timestamp);
$month = date('m', $timestamp);
$day = date('d', $timestamp);
$newTimestamp = mktime(0, 0, 0, $month, $day, $year);
DateTime class is pretty good for this kind of task
<?php
// for PHP5.2+ though
var_dump(date_create('2011-9-12 05:48:00')->setTime(0, 0, 0)->format('U'));
// if input is in integer already
$input = 1315806480;
var_dump(date_create("#{$input}")->setTime(0, 0, 0)->format('U'));
<?php
$time = strtotime(date('Y-m-d', *timestamp*));
?>
EDITED:
The date() function takes your timestamp and represents it in a human readable string with only the year, month, and date (i.e. "2014-07-25") and omitting the hour, minutes, and seconds.
Then the strtotime() function takes that human readable string and turns it back into your timestamp, effectively making the time at 00:00 on that day.
Related
I am trying to get a numeric value (0-6) for the last day of the week in the month however it keeps returning a value of 3 when it in fact should be 4 for the month of 05-2018. Am I going about this in the wrong manner?
$endMonth = date('Y-m-t');
$dayPostition = date('w', $endMonth);
If you want to get the last on current month, you can:
$date = new DateTime("now");
$date->modify('last day of this month');
echo $date->format('w');
Or using a specific date
$date = new DateTime("2018-05-01"); //May 1, 2018
$date->modify('last day of this month');
echo $date->format('w');
The problem is that the second parameter of date() must be a timestamp rather than a string, and date() itself simply returns a string.
As such, you need to explicitly convert the first date() to a timestamp with strtotime():
$testvar = date('Y-m-t');
$lastDay = date('w', strtotime($testvar));
Which can be seen working here.
You are sending a string, not a date... try this
$endMonth = date('Y-m-t');
$dayPostition = date('w', strtotime($endMonth));
If you want to get the last day of month, you can use cal_days_in_month()
This function will return the number of days in the month of year for
the specified calendar."
http://php.net/manual/en/function.cal-days-in-month.
Try this
$getlastday = cal_days_in_month(CAL_GREGORIAN, 05, 2018);
$endMonth = date('2018-05-'.$getlastday);
$dayPostition = date('w', strtotime($endMonth));
I store user register date and time as datetime in MySQL. now to do some process with that date i need to get year, month and day separately to work with.
for example:
2015-07-30 19:20:34
now I want 2015, how do I do that in PHP?
date('Y', strtotime(<date string from mysql>));
The strtotime function (http://php.net/manual/en/function.strtotime.php) parses your date string to a Unix timestamp, and then the date function (http://php.net/manual/en/function.date.php) outputs it in the defined format.
So you can do something like:
$date = '2015-07-30 19:20:34';
$year = date('Y', strtotime($date));
I'm not sure but try this:
<?php
$str="2015-07-30";
$explode=explode("-",$str);
echo $explode[1];
?>
$datetime = date_create("2015-07-30 19:20:34");
$day = date_format( $datetime, 'l'); // Thursday
$date = date_format( $datetime, 'd'); // 30
$month = date_format( $datetime, 'F'); // July
$year = date_format( $datetime, 'Y'); // 2015
function.date php
datetime.format.php
It is very simple, you can use strtotime() function of PHP like this:
$dated=$row['timestamp'];// in this case '2015-07-30 19:20:34';
$FullYear=date('Y',strtotime($dated)); // result 2015
$FullYear=date('y',strtotime($dated)); // result 15
date('Y', strtotime(<date string from mysql>));
$date = '2015-07-30 19:20:34';
$year = date('Y', strtotime($date));
It should work.
I have some data that makes use of date("j/n/y") format i.e date for today is 23/1/15
I have tried
echo strtotime($today);
but this does not give me the timestamp i want.How would i convert a date in date("j/n/y") format to epoch?.
Use DateTime::createFromFormat() to read the date format and then use DateTime::getTimestamp() to format it as a timestamp.
$date = DateTime::createFromFormat('j/n/y', '23/1/15');
$epoch = $date->getTimestamp();
I think you're looking for the mktime function in PHP. It goes a little like this:
$timestamp = mktime(0,0,0,0,0,0);
Where, in order, the arguments are: hour, minute, second, month, day, year. So, in your case:
$today = mktime(0, 0, 0, 1, 23, 2015);
// Would return the timestamp for Jan. 23rd, 2015 at 12:00:00 am (I think)
If you're looking for a dynamic right now timestamp, you may use date() in each of the arguments of mktime. For example:
$rightnow = mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y"));
// Would return the timestamp for Jan. 23rd, 2015 at 10:57:25 am.
But, as John Conde says, it requires you break apart the date before you can use it, so it may not be as efficient.
Hope that helps!
Just to have another approach this one would be good for 85 more years.
$date = date('j/n/y', time());
list($day, $month, $year) = explode("/", $date);
$date = "20" . $year . "-" . $month . "-" . $day;
echo date('m/d/Y', strtotime($date));
I'm trying to extract just the day of the month from the dateTime formatted as follows:
$today = date('Y-m-d\TH:i:sP', strtotime('today'));
But, when I try extracting just the day of the month, I get '31'.
I'm using: $day = date('d', $today);
Which, I'm guessing, is incorrect.
This is because the second parameter to date() needs to be a Unix Timestamp. You're passing it string. As a result you get a date of Dec 31, 1969.
All of that code is unnecessary anyways as all you need is:
$day = day('d');
If you're going to only have access to the date string you must convert it to Unix Timestamp before passing it to date().
To extract days (or other parts of a datetime), I use the format function:
$datetime = new DateTime('2000-01-10', new DateTimeZone('Pacific/Nauru'));
$day = $datetime->format('d');
echo $day;
Use any format form the PHP Manual.
Hope this will solve your problem
$today = date('Y-m-d\TH:i:sP', strtotime('today'));
$day = date('d', strtotime($today)); // here is the difference,
// instead of $today use strtotime($today)
I have a month value (1-12), day value (1-31), and a year value (2010,2011,2012). I also have a hour value and a minute value.
How can I give this to strtotime() in a way it can convert it to a timestamp?
why convert string to date when you already know year month and date.
use setDate funtion
<?php
$date = new DateTime();
$date->setDate(2001, 2, 3);
echo $date->format('Y-m-d');
?>
Given the variables $year $month $day $hour $minute you can do:
strtotime("$year-$month-$day $hour:$minute");
Be careful to enclose the variables with ", never in this case with '.
UPDATE (thanks to comments of #Clockwork and #Tadeck):
In case there is a variable $timeofday that represents the time of day (i.e. AM or PM),
then you can parse it this with:
strtotime("$year-$month-$day $hour:$minute$timeofday");
that is to say, just attach that variable to the end of the text.
Is strtotime the best tool for this job? What about mktime()?
$time = mktime($hour, $minute, 0, $month, $day, $year);
You can provide it to function strtotime() in many ways, as mentioned in documentation. Some examples include:
$your_time = strtotime('12/31/2011 9:59');
$your_time = strtotime('2011-12-31 9:59');
$your_time = strtotime('December 31, 2011 9:59');
etc. It really is very flexible.
You can find the list of valid formats in the documentation, and that is (from the "Compound Formats" list in the mentioned documentation) for example:
10/Oct/2000:13:55:36 -0700,
2008:08:07 18:11:31,
2008-08-07 18:11:31,
2008-07-01T22:35:17.02,
2008-07-01T22:35:17.03+08:00,
20080701T22:38:07,
20080701T9:38:07,
20080701t223807,
20080701T093807,
2008-7-1T9:3:37,
(this is really copy of the documentation)
Use it like this strtotime("YYYY-mm-DD HH:MM AM/PM"):
echo date("d F Y h:i:s A", strtotime("2011-06-01 11:15 PM")) . "\n";
OUTPUT
01 June 2011 11:15:00 PM
Y-m-d hh:mm will work
echo strtotime('2011-12-14 11:44 am');
cit #Pekka :)
strtotime($month."-".$day."-".$year)