PHP Front To Back [Part 8] - Dates & Timestamps

Sharing buttons:

you guys welcome to your next PHP

front-to-back project or not project bug

video and in this video we're going to

be talking about date okay we're going

to look at the date function which is a

very important function as well as a

couple others so I'm going to create a

new file here and let's go ahead and

let's save this as date dot PHP okay I'm

just going to go on my browser to local

host PHP sandbox slash date dot PHP so

let's throw in our tags and let's go

ahead and just echo out date the date

function and see what happens so by

default it's going to give us a warning

because we didn't put any parameters in

so it doesn't know what to give us we

can basically dissect to the date and

time and return whatever we want from it

okay so if we were to put in a lowercase

D that's going to give us well that's

going to give us the day so let's try

that you can see I get oh one and the

date today is March 1st 2017 so it gives

us the number of the day we can also do

the month comment that out and for this

let's say month and reload and we get 3

okay because it's March if we want to

get the year we can do an uppercase Y

and that's the year so it's comment this

out and that should give us 2017 we can

even get the day of the week so if we

say echo date and we use a lowercase L

that'll give us

day of the week okay so if we reload

that we get Wednesday okay so now we can

combine these as well which is usually

what you want to do so let's do echo dig

and let's say we want the format of year

month date so let's do Y / m / B and if

we take a look at that it gives us 2017

3-1 okay if we want to use the format

month/day/year then we can just do month

day year okay and you can use whatever

you want in hand for instance if you

wanted to use dashes or hyphens you

could do that as well okay whatever you

want so in addition to working with date

the date function also works with time

so if we want to say echo date and let's

pass in here and H that will give us the

hour all right so let's reload and it

gives us o - now that's actually wrong

for me and I'll tell you why and I'll

play how to fix it in a second I just

want to show you that we can also do the

minutes okay so minute is going to be an

I call it that out okay so that gives us

five and then we can also get the

seconds with an S

gives us 50 and then we can also get

whether it's am rpm with an a comment

that out and save that reload and we get

PM okay so we can also combine these

together so let's do echo the echo date

and pass in here I'll say H pull in I :

s a so let's see what that gives us so

that gives us two oh six pm in 24

seconds now if you look at my time down

here it's actually eight oh six a.m.

so what we need to do is define the

timezone okay because for some reason my

system my PHP and my xampp is set to a

different time zone so it's a set time

zone and for that we used 8 underscore

default underscore our time zone get or

set rather if we want to get it we can

use get and here you want to put the

time zone so in my case is America / new

underscore York ok for the semicolon and

you load see that's invalid oh this

shouldn't be a space there should be a

slash okay so now we get the correct all

right so we can also make date and times

so I'm going to show you a function

called make time or m'kay time what I'm

going to do is set a variable called

time stamp and we're going to set this

to m'kay time and I'm going to set let's

say 1014 54 9

n and then 1981 so this is actually my

birthday so it's hours minutes seconds

month/day/year okay so let's see what

that gives us if we say echo timestamp

it gives us the string of numbers all

right you might not have been expecting

that you're probably expecting a date

well a timestamp what's that is let me

just paste this in a UNIX timestamp is a

long integer containing the number of

seconds between the UNIX epoch which is

January 1st 1970 and then to the

specified time - to whatever today is or

whatever not in this case in this case -

whatever this is ok so that's the number

of seconds that have passed since that

date and that gives us an exact time

that we can use all around the world ok

now if we want to change this to an

actual readable date we can do that okay

we can do that using the date function

so let's do echo date and let's say we

want it in the format of month day year

and then we can pass in a second

parameter of a timestamp guys let's pass

that in and then we reload and we get 9

10 81

ok we could also add on H I a and it

also gives us the time okay so we can

make a timestamp with make time and then

we can convert that into a readable date

using the date function all right so the

next function we're going to look at is

string - time or STI - time and it just

does just that'll take a string and turn

it into a time and there's a whole bunch

of different strings we can use or

different formats we can use so let's

say time stamp - and we'll set it to STI

- time and here let's input show your

single quotes let's do 7 o'clock p.m.

I'll say March 22nd 2016 okay so that's

a string now if we echo out timestamp -

let's see what we get we get a timestamp

for that date all right and then we can

take that timestamp and we can put it

through the date function so let's echo

date and let's see we'll just grab this

formatting right here and actually I

will do this time as well I mean yeah

the time and then it's going to take a

second parameter of timestamp - all

right let's take a look at that we want

to comment this out though and that

gives us three 22 2016 7 p.m. now the

string that we passed in here is very

specific but we can be much more vague

so let's say time stamp 3 and I'll sort

of - string to time and in here let's

just put the string tomorrow and I'll

change this right here to time stamp 3

reload and it gives us tomorrow's date

today's the first and it's giving us a

second okay we can also do days of the

week so for instance I'm just going to

copy this and it's changed out the

timestamp for and then let's say next

Sunday it'll actually take that and give

us a date change this to a timestamp for

reload now we get 3/5 it's going to give

us the next the next coming Sunday ok we

can also do let's say plus 2 months

times 10 to 5 reload and it gives us two

months from now we could also do plus

two days or years or anything like that

ok so it's very flexible as to what you

can pass in here and get a timestamp

from all right so those are the basics

if you want to look into this more to

look at how you can format dates then I

suggest you go to the documentation so

you could go to php.net slash manual

slash English for me and then function

dot and then you can look up any

function so date dot PHP ok and then

down here you can see all the different

formatting rules that we can use I only

showed you a couple of them but there's

actually a ton of them alright so you

may want to check that out and there's

some examples as well but yeah the

documentation for PHP is pretty damn

good it has definitely saved me and

helped me quite a few times ok so that's

it for this video guys we will be using

you know we'll be using dates throughout

the series so that's going to wrap it up

and thanks for watching and I'll see you

in the next video